static char* example[]
declares example
as an array of pointers to char
. The array is initialized to three string literals, so example[0]
is "doctor"
, example[1]
is "who"
, and example[2]
is "hello"
.
Since example
is an array, the array identifier example
is going to evaluate to the address of the array's first element. If you try something like this:
printf ("%p %p %p\n", example, &example, &example[0]);
you'll see that they all have the same value. All these, however, are semantically different types.
example
has the type array of pointers to char
&example
has the type pointer to array of pointers to char
&example[0]
has the type pointer to pointer to char
.
Each element of the array has its own address. Try this:
printf ("%p %p %p\n", &example[0], &example[1], &example[2]);
The first will be the same address as the array, but the others will be offset from that address by the size of a pointer on your system (typically four bytes for a 32-bit system, 8 bytes for a 64-bit system).
The char
that each pointer in your array example
is pointing to is the first char
of a string literal. Each string literal has its own address, probably in a read-only memory segment. You can try this too:
printf ("%p %p\n", &example[0], example[0]);
&example[0]
is the address of the first pointer in the array of pointers to char
.
example[0]
is the first pointer in the array of pointers. Think of an array of int
. Each element of that array would have an address and a value, the latter being an int
. Since example
is an array of pointers, each element of example
is going to have an address and a value, the latter being a pointer.
You can repeat the exercise for &example[1]
, etc:
printf ("%p %p\n", &example[1], example[1]);
printf ("%p %p\n", &example[2], example[2]);
To sum up:
- The array of pointers to
char
has the same address as its first element.
- Each subsequent element, i.e., each subsequent pointer in the array, has its own address.
- Each of those pointers points to (the first
char
of) a string, which has its own address.
Hope that's clear.