In some code that I read, there was an initializing statement like this
char *array[]= { "something1", "something2", "something3" };
What does this mean, and what does that pointer actually point to?
How is that allocated in memory, and how can I access every element and every character of an element in that array ?
— Edited —
and please what is the difference in this example between
char array[3];
and
char *array[3];
— Edited —
It’s initializing an array of strings (
char *) with three values (three pointers to null-terminating strings)It should point to the first element in the
char*arrayIt will allocate enough memory to store the three strings followed by null-terminators, as well as the three pointers to those strings:
Note that the strings may not necessarily be in sequential memory
if by “element” you mean the string, you can loop though the pointers:
well, you could access the chars using array syntax (
element[i]) but I would recommend using the C string functions for safety (so you don’t have to worry about accessing memory outside the range of the string)