1

In the following function declaration, the first argument is a String, specifically, an array of chars, and the third argument is a pointer to an integer. Is the second argument a pointer to an array of chars? In other words, a pointer to a pointer? I find this after reading this answer to a related question: Difference between passing array and array pointer into function in C

void setup(char inputBuffer[], char *args[], int *background) {...}

In other words, is *args[] equivalent to **args?

Thanks a lot!

Community
  • 1
  • 1
macalaca
  • 988
  • 1
  • 13
  • 31
  • 2
    You might want to learn about [the spiral/clockwise rule](http://c-faq.com/decl/spiral.anderson.html). – Some programmer dude Jan 19 '15 at 20:23
  • That is incredible, thanks @JoachimPileborg – macalaca Jan 19 '15 at 20:27
  • I'm quite sure in function declaration it is indeed equivalent. – MightyPork Jan 19 '15 at 20:29
  • http://stackoverflow.com/questions/779910/should-i-use-char-argv-or-char-argv-in-c – musarithmia Jan 19 '15 at 21:17
  • 1
    A pointer to an array of chars is `char (*ptr)[]`. An array of pointers is `char *ary[]`. `ary` is the same as `char **ary`because it decays to a pointer. `ptr` cannot decay because it is not an array itself necessarily. The difference can be important. –  Jan 20 '15 at 00:40

2 Answers2

1

*args[] is equivalent to **args. While passing an array as function argument, pointer to the beginning of array is passed (e.g. first element). Therefore, you don't know the size of array passed to function and usually size of array is passed in another function's argument.

In your specific case char* args[] is an array of string literals. For better understanding of that mechanism see question 6.4 from C-FAQ. This link implies that :

Since arrays decay immediately into pointers, an array is never actually passed to a function.

macfij
  • 3,093
  • 1
  • 19
  • 24
1

Yes, in passing arguments to a function, char *args[] is equivalent to char **args.

In the first argument, char inputBuffer[], the function actually receives not the whole char array but only a pointer variable holding the address of its first element.

In the second argument, char *args[], similarly, the function receives not the whole array of pointers to chars, but a pointer variable holding the address of the first element. In this case the element is itself a pointer. Therefore the function receives a pointer to a char pointer, equivalent to char **args.

musarithmia
  • 216
  • 3
  • 9