[ACCEPTED]-C difference between *[] and **-pointers

Accepted answer
Score: 24

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

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

char a[20];

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

extern char a[];

but this will 4 not:

extern char *a;

If we make it an array of pointers instead:

char *a[20];

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

extern char *a[]; // works

extern char **a;   // doesn't work
Score: 10

Depends on context.

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

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

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

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

Score: 5

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

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

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

Score: 3

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

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

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

Score: 2

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

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

Example:

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

This really depends on the context of where the 29 declarations occur.

Outside of a function 28 parameter definition, the declaration

T a[];

declares 27 a as an unknown-sized array of T; the array type 26 is incomplete, so unless a is defined elsewhere (either in 25 this translation unit or another translation 24 unit that gets linked) then no storage is 23 set aside for it (and you will probably get an "undefined 22 reference" error if you attempt to link, although 21 I think gcc's default behavior is to define 20 the array with 1 element) . It cannot be 19 used as an operand to the sizeof operator. It 18 can be used as an operand of the & operator.

For 17 example:

/** 
 * module1.c
 */
extern char *a[]; /* non-defining declaration of a */

void foo()
{
  size_t i = 0;
  for (i = 0; a[i] != NULL; i++)
    printf("a[%lu] = %s\n", (unsigned long) i, a[i++]);
}

module1.c uses a non-defining declaration of 16 a to introduce the name so that it can be 15 used in the function foo, but since no size 14 is specified, no storage is set aside for 13 it in this translation unit. Most importantly, the 12 expression a is not a pointer type; it is an incomplete array 11 type. It will be converted to a pointer 10 type in the call to printf by the usual rules.

/**
 * module2.c
 */
char *a[] = {"foo", "bar", "bletch", "blurga", NULL}; /* defining declaration of a */

int main(void)
{
  void foo();

  foo();
  return 0;
}

module2.c 9 contains a defining declaration for a (the size of 8 the array is computed from the number of 7 elements in the initializer), which causes 6 storage to be allocated for the array.

Style 5 note: please don't ever write code like 4 this.

In the context of a function parameter 3 declaration, T a[] is synonymous with T *a; in both 2 cases, a is a pointer type. This is only true 1 in the context of a function parameter declaration.

Score: 1

As Paul said in the comment above, it's 11 syntactic sugar. Both char* and char[] are 10 the same data type. In memory, they will 9 both contain the address of a char.

The array/index 8 notation is equivalent to the pointer notation, both 7 in declaration and in access, but sometimes 6 much more intuitive. If you are creating 5 an array of char pointers, you may want 4 to write it one way or another to clarify 3 your intention.

Edit: didn't consider the 2 case Jerry mentioned in the other answer. Take 1 a look at that.

Score: 1
    char *ptr[2]={"good","bad"}; //Array of ptr to char
    char **str;  //Refer ptr to ptr to char
    int i;
    //str = &ptr[0];  //work
    str = ptr;

    for(i=0;i<2;i++)  printf("%s %s\n",ptr[i],str[i]);

Its o/p same. Using that we can easily understand.

0

More Related questions