C difference between *[] and **

Solution 1:

Under the circumstances, there's no difference at all. If you try to use an array type as a function parameter, the compiler will "adjust" that to a pointer type instead (i.e., T a[x] as a function parameter means exactly the same thing as: T *a).

Under the right circumstances (i.e., not as a function parameter), there can be a difference between using array and pointer notation though. One common one is in an extern declaration. For example, let's assume we have one file that contains something like:

char a[20];

and we want to make that visible in another file. This will work:

extern char a[];

but this will not:

extern char *a;

If we make it an array of pointers instead:

char *a[20];

...the same remains true -- declaring an extern array works fine, but declaring an extern pointer does not:

extern char *a[]; // works

extern char **a;   // doesn't work

Solution 2:

Depends on context.

As a function parameter, they mean the same thing (to the compiler), but writing it char *argv[] might help make it obvious to programmers that the char** being passed points to the first element of an array of char*.

As a variable declaration, they mean different things. One is a pointer to a pointer, the other is an array of pointers, and the array is of unspecified size. So you can do:

char * foo[] = {0, 0, 0};

And get an array of 3 null pointers. Three char*s is a completely different thing from a pointer to a char*.

Solution 3:

You can use cdecl.org to convert them to English:

  • char *argv[] = declare argv as array of pointer to char

  • char **argv = declare argv as pointer to pointer to char

Solution 4:

I think this is a little bit more than syntactic sugar, it also offers a way to express semantic information about the (voluntary) contract implied by each type of declaration.

With char*[] you are saying that this is intended to be used as an array.

With char**, you are saying that you CAN use this as an array but that's not the way it's intended to be used.

Solution 5:

As it was mentioned in the other answers, char*[] declares an array of pointers to char, char** declares a pointer to a pointer to char (which can be used as array).

One difference is that the array is constant, whereas the pointer is not.

Example:

int main()
{
    char** ppc = NULL;
    char* apc[] = {NULL};
    ppc++;
    apc++; /* this won't compile*/
    return 0;
}