Home > Net >  C -- pointers to pointers VERSUS array of pointers
C -- pointers to pointers VERSUS array of pointers

Time:04-12

In C, why can't I write:

char **expectedPINs01 = { "0", "5", "7", "8", "9" };

Cause I got:

warning: initialization of ‘char **’ from incompatible pointer type ‘char *’

But it is possible to write:

char *expectedPINs01[] = { "0", "5", "7", "8", "9" };

What's the difference?

CodePudding user response:

When you write char ** you are getting enough space for one pointer. If you want this to behave like an array, you then have to malloc space sufficient for the size of the array and fill in the entries.

When you write char *x[5] you get enough space for 5 pointers.

When you use the initialization shorthand, you can omit the 5 because the compiler can see from your initializer that you want there to be space for 5 pointers, and it reserves enough space for them and then uses the initializer shorthand to fill them in for you.

  • Related