I'm new in c++ and don't know much about using pointers.
What I actually know is how to declare a pointer like this:
1 2 3 4 5
char var = 'a';
char* ptr;
ptr = &var;
cout << *ptr; // --> a
cout << ptr; // --> address of var a (like 0x0000)
I also understand that the declaration and initialization of ptr can be done in one step:
1 2 3 4
char var = 'a';
char* ptr = &var;
cout << *ptr; // --> a
cout << ptr; // --> address of var a (like 0x0000)
What I don't understand is the following:
char* ptr[] = {"Hello", "World"};
Why is it possible to get the string "Hello" by calling it via: ptr[0]
??
I thought that typing a pointer without "*" only means to work with the address of the pointing variable/array --> so why is ptr[0] not the address of the array with the string "Hello", but instead it is the string itsself (what should be *ptr[0] or am I wrong??)...
This is confusing me much and I hope someone can help me with that...
If you run your first example you will notice that the result is not what you expect. The output stream operator for char* is a bit special. It will not print the address but instead it will assume that the pointer points to the first character in a C string and print the whole string.
What I simply don't understand is why I don't have to initialize the pointer to an array with the address of the array like this:
1 2 3 4 5
char var[] = {'a', 'b', 'c'};
char* ptr = &var; // <--- won't work, but I don't understand why not...
// --- instead, I have to initialize it with the real array:
char* ptr = var;
How is that possible? How does the pointer know where to point? Why doesn't point the pointer in this example to the unreal address 'a' ?
Ok, that makes sense. But in the example above, there is no array like 'var' that could be used to initialize ptr, instead, ptr was directly initialized like an array:
char* ptr[] = {"Hello", "World"};
Depending on your argument, my first thought was that initializing the ptr with the array content directly also returns the address of the first array element, but the following won't work:
char* ptr = {'a', 'b', 'c'};
So why does the code above ("Hello World") work but this example crashes?
char* s is a pointer to one (or more) character(s). You can treat it as an array.
char s[12]; is an array of 12 characters. s reduces to a pointer to a character.
char* s[12]; is an array of 12 pointers to character. Hence:
1 2 3 4
char* strings[] = { // the array of pointers to string (character arrays)
"Hello", // the first string (character array)
"world" // the second string (character array)
};
As an array of pointers to strings (an array of arrays, if you will), you can then say things like:
if (strings[0][0] != 'H') cout << "uh, no Hello?\n";
Thanks for the link, I'll take a look at it soon :)
That is what I also thought, but I don't understand why to use pointers here?
Normally, I initialize a pointer with an address, but in this case I initialize it with content. How does this work?
Why doesn't string[0] point to a non-existing address "Hello" in this example? I thought I have to use *string[0] for content and string[0] for storing addresses... I didn't get it so far -.-
arr[x] is absolutely equyivalent to *(arr + x) (By Standard).
Even 0[arr] would work. Try it, it is fun.
*string[0] is equivalent to **(string + 0) <=> **string or string[0][0];
Why doesn't string[0] point to a non-existing address "Hello" in this example?
Why non-existent? String literal "Hello" does exist inside your program and has an address. But you should use constchar* actually, as string literals are stored in read-only mempry and attempt to change them can lead to crash.
Arrays has own type, but do not have many operations allowed with them. But they decays to pointers quite easily and pointers allows way more operations.
Wow, I followed the link above and read a lot about pointers and arrays, then I tried some strange stuff like this:
1 2
char var[] = {'a', 'b', 'c'};
cout << *var;
... and what happens --> it works!! That is really confusing, are pointers the same thing as arrays and arrays are pointers?? That would explain everything to me...
EDIT:
@MiiNiPaa:
Thanks, I just realized your post to late... That is really funny, I can't see a real difference between arrays and pointers right now, but I do understand the initialization now..
Why non-existent? String literal "Hello" does exist inside your program and has an address
That is not what I meant, I meant that in this case, you are storing the word "hello" into the pointers buffer where an address to a variable should be stored, so why doesn't point the pointer to the address "hello" now? Do you understand what I mean?
Example:
1 2 3 4
char var = 'a'; // var has address "0x002244"
char* ptr = &var; // ptr points to "0x002244"
char* ptr = "Hello"; // ptr should point to (non-existing) address "0xHello"
Do you know what I mean? How is it possible to store data in a pointers address field?
are pointers the same thing as arrays and arrays are pointers??
No. Arrays transforms to pointers easily, but not backward.
and what happens --> it works!!
Yes. It is equivalent to char var[3] = {'a', 'b', 'c'};. Compiler just deduct number from initializer.
char* var = {'a', 'b', 'c'}; does not compile, because left expression is not an array and therefore right expression will not be threated as array initializer. *var is equivalent to var[0] so it returns 1st element: 'a'