As we know, array name can’t be assigned, sentence like:
char * array[], * point;
array = point; /* wrong */
array++; /* wrong */
But in main(int argc, char * argv[]), argv++ is ok and works well.
What do i missing?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the context of a function parameter declaration,
T a[]andT a[N]are both interpreted asT *a; in all three cases,ais declared as a pointer toT, not an array ofT. Thus, inint main(int argc, char *argv[]),argvis really declared aschar **, or pointer to pointer tochar, not array of pointer tochar.(edit — note that this is true only for function parameter declarations; for a regular variable declaration,
T a[N]andT a[]both declareaas an array ofT).Since it’s a pointer value, it can be assigned to and it can be incremented.
Beyond that, here’s what the language standard has to say:
EDIT
And here’s the language for function parameters:
EDIT2
Some examples (assumes a C99 compiler):
One more piece of standardese:
In the function
main,a1anda2have been declared as 5-element arrays ofint;a2gets its size from the number of elements in the initializer. The expressionsa1anda2thus have types “5-element array ofint” and they may not be targets of an assignment expression, nor may they be operands to the++or--operators. When these expressions appear in the call tofoo, their types are converted to “pointer toint” per the rule above. Thusfooreceives a pointer value, not an array value, fora(which is covered by the rule that says array parameters are converted to pointer types). So the expressionainfoohas typeint *, or pointer toint; thus,amay be the target of an assignment, and it may be an operand of++and--.One more difference: per the rule quoted above, the conversion to a pointer type doesn’t happen when the array expression is an operand of the
sizeofoperator;sizeof a1should evaluate to the number of bytes taken up by the arraya1(5 * sizeofint). However, sinceainfoohas typeint *, notint [5],sizeof ashould only evaluate to the number of bytes for an pointer toint(sizeof(int *)).