Question:
How is "int main(int argc, char **argv);" similar to/different from "int main(int argc,char *argv[]);" in C?
Butters
2012-07-31 06:48:05 UTC
From what I know about the later, ie "int main(int argc,char *argv[]);" that argc means the number of string input to the main() function while *argv[] is an array of pointers to the string inputs. Can you explain to me what the first statement , ie "int main(int argc,char **argv);" means then AND why is "char *argv[]" an array of pointers to strings rather than an array of pointers to characters?Is an array of pointers to single characters same as an array of pointers to strings?If not, what is its syntax then?
Three answers:
anonymous
2012-07-31 07:39:30 UTC
C has no string type at all. There are no ``pointers to strings'' either. ``char **argv'' has the type ``pointer to pointer to char'', and it is guaranteed (by the standard) to point at an array of pointers to char that is terminated with a null pointer. Each pointer to char will point at a string, which is an array of chars that is terminated with the null character ('\0').



argv has the same type in both of the declarations you've listed since you cannot pass array types to functions. Consider the following declarations:



void x(int a[]);

void y(int b[10]);



Both of these declarations accept a pointer to int. While y provides an explicit boundary, it has no effect on the program's functionality, it only gives the user an idea of how the passed parameter will be used in the function. You can verify this for yourself by observing how ``sizeof a'' in x and ``sizeof y'' in y will both yield the same value as ``sizeof (int *)''.



edit:

> How does it point to an ARRAY of pointers to char while there is no "[]" symbol?

Sorry. It doesn't point to an array, but it points to the first element of one. This means that:



argv[0] = first pointer. Either the name of the program, an empty string, or a null pointer if argc == 0.

argv[1] = second pointer. Typically the first argument given, or a null pointer if argc == 1.

...

argv[argc] = A null pointer.



> And what exactly you mean by "guaranteed by the standard"?

The C implementation is guaranteed by the standard to pass a pointer to the first element of an array of pointers to char, which is terminated with a null pointer, to main.



> "Each pointer to char will point to a string"?How come it will point to a string instead of consecutive characters?

> After all string is an array or characters terminated by a "\n"



A string is an array of chars (or consecutive chars) that is terminated with the null character ('\0').
kbb
2012-07-31 14:05:49 UTC
For clarification :-

char *argv[] :- argv is an array of character pointers

char **argv :- argv is an pointer to character pointers



In sense both are very much the same

Now string is actually not native or elementary data type but rather formed by beginning indicated by a character pointer and delimited by a null character .



hope it is clear now!!
James Bond
2012-07-31 14:09:36 UTC
char **argv;

This is pointer to a character pointer.

Basically in the memory a character pointer array will be created. Its based address is the argv. Rather, first element of this pointer array is also character pointer. Its address is assigned to agrv. Thus, argv is declared as character pointer to pointer.



Now, whatever arguments we give along the command line are stored in memory their addresses (character pointers) will be stored in the above character pointer array. Ofcourse, first string is program name itself.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...