简体   繁体   中英

Passing a pointer to a char array as an argument to a function - C

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!

*args[] is equivalent to **args . While passing an array as function argument, pointer to the beginning of array is passed (eg 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.

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 char s, 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM