Why does this C code example use a pointer to a pointer?
The following code example demonstrates the similarities between pointers and arrays. In the second method, the author declares cur_name as a pointer to a pointer. My question is, why is this necessary? Why is it declaring a new pointer instead of just using the original pointer names? I should note that the code compiles and works fine when I get rid of cur_name and use names, so this is a matter of style and not a function? Any explanation would be appreciated.
#include <stdio.h>
int main(int argc, char *argv[])
{
// create two arrays we care about
int ages[] = {23, 43, 12, 89, 2};
char *names[] = {"Alan", "Frank", "Mary", "John", "Lisa"};
// safely get the size of ages
int count = sizeof(ages) / sizeof(int);
int i = 0;
// first way using indexing
for(i = 0; i < count; i++) {
printf("%s has %d years alive.\n",
names[i], ages[i]);
}
printf("---\n");
// setup the pointers to the start of the arrays
int *cur_age = ages;
char **cur_name = names;
// second way using pointers
for(i = 0; i < count; i++) {
printf("%s is %d years old.\n",
*(cur_name+i), *(cur_age+i));
}
printf("---\n");
// third way, pointers are just arrays
for(i = 0; i < count; i++) {
printf("%s is %d years old again.\n",
cur_name[i], cur_age[i]);
}
printf("---\n");
// fourth way with pointers in a stupid complex way
for(cur_name = names, cur_age = ages;
(cur_age - ages) < count;
cur_name++, cur_age++)
{
printf("%s lived %d years so far.\n",
*cur_name, *cur_age);
}
return 0;
}
source to share
The code uses a pointer to a pointer to represent a pointer to a C string array. Namely, cur_name
is a pointer to a C string array called names
. Since a string in C is itself represented by a pointer to char
, a pointer to an array of such pointers becomes a pointer to a pointer.
Why is it declaring a new pointer instead of just using the original pointer
names
?
Since names
it is not a pointer, it is an array of pointers (see the square brackets after the declaration? This is what the array does names
. The only asterisk in front is associated with the type of the array element, which is char*
in this program).
Create an array adds a level of indirection: to specify the array int
you want int*
, but to specify the array int*
you need a pointer int**
.
source to share
If you have an array of type T below
T a[N];
then if you want to define a pointer to your first element you should write
T *p = a;
Now, replace T for the type of array elements names
defined as
char *names[] = {"Alan", "Frank", "Mary", "John", "Lisa"};
You'll get
typedef char * T;
T names[] = {"Alan", "Frank", "Mary", "John", "Lisa"};
so the pointer to the first element of the array will look like
T *cur_name = names;
If you do the reverse replacement now, you will receive
char * *cur_name = names;
^^^^^^
T
source to share
The author increases them in the fourth method, so he needs the temporary data for this, but you are not sure if he is not needed in the second method. Perhaps he is using the assignment to show their equivalence?
I think the examples would be clearer if each method were implemented in its own function.
source to share