C Programming Arrays Of Pointers
Declaration
An array of pointers is an ordinary array variable whose elements happen to be all pointers.
This creates an array of four pointers to char. The array p[] itself is like any other array. The elements of p[], such as p[1], are pointers to char.
Initialization
Initializing an element of an array of pointers is just like initializing ordinary pointers, but we must include the array index. This example assigns the address of x to the pointer in the element of the array.
This next example assigns the address of the first character in the string to the pointer in element 0 of the array.
This example shows how you would populate the array of pointers in the following picture.
2 p[1] = "Off";
3 p[2] = "Main";
4 p[3] = "Aux";
This would create the strings in memory and initialize each of the pointers in the array to point to a particular string.
Dereferencing
To use the value pointed to by a pointer array element, just dereference it like you would an ordinary variable:
Using *p[0] is the same as using the object it points to, such as x or the string literal "My String" from before.
Accessing Strings
Example
2 char *str[] = {"Zero", "One", "Two", "Three", "Four", "\0"};
3
4 int main(void)
5 {
6 while(*str[i] != '\0')
7 printf("s\n", str[i++]);
8 while(1);
9 }