Originally Posted by
iMalc
Not quite. It actually just declares a pointer, and then initialises that pointer with a value that happens to point to the data segment of your program where the characters 'H', 'e', 'l', 'l', 'o', '\0' are stored sequenially (and cannot bechanged).
You can verify the differences between these by using 'sizeof'.
char name1[10] = "hello";
int s1 = sizeof(name1); // s1 is 10
char name2[] = "hello";
int s2 = sizeof(name2); // s2 is 6
char *name3 = "hello";
int s3 = sizeof(name3); // s3 is 4 (assuming 32-bit pointers)
About: "char *argv[]" read type declarations from right to left. This is an array (unknown length), of pointers to char. Each one of those pointers can point to any number of characters it likes. the array of pointers is as long as argc says it is.
Something that you may also find interesting is that in the case of jsut declaring a pointer, where the actual characters live in the data segment, the compiler is free to overlap and reuse portions of the data segment because it can assume that it is never modified. E.g. if your code contained pointers to the strings: "world", "d", "hello world", and "ld", even if some of those appeared numerous times, then the data segment could get away with just storing:
'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '\0',
You'll notice that all the shorter strings appear within this data, followed by the nul-character, and pointers to those strings can simply point to the appropriate characters inside this data. The string "hello" would need to be stored separately because it is followed by a '\0' whereas in the above is followed by a space.