I’m currently learning C through “Learning C the Hard Way“
I am a bit confused in some sample code as to why some arrays must be initialized with a pointer.
int ages[] = {23, 43, 12, 89, 2};
char *names[] = {
"Alan", "Frank",
"Mary", "John", "Lisa"
};
In the above example, why does the names[] array require a pointer when declared? How do you know when to use a pointer when creating an array?
A string literal such as
"Alan"is of typechar[5], and to point to the start of a string you use achar *."Alan"itself is made up of:As you can see it’s made up of multiple
chars. Thischar *points to the start of the string, the letter'A'.Since you want an array of these strings, you then add
[]to your declaration, so it becomes:char *names[].