Why can argv array name be assigned?

As you know, an array name cannot be assigned, for example:

char * array[], * point; array = point; /* wrong */ array++; /* wrong */ 

But in main(int argc, char * argv[]) , argv++ fine and works well. What am I missing?

+7
source share
3 answers

In the context of declaring a function parameter, T a[] and T a[N] interpreted as T *a ; in all three cases, a declared as a pointer to T , not an array of T Thus, in int main(int argc, char *argv[]) argv indeed declared as char ** or as a pointer to a pointer to char , and not to an array of a pointer to char .

( edit ) - note that this is only true for declaring function parameters, for declaring a regular variable, T a[N] and T a[] declare a as an array of T ).

Since this is a pointer value, it can be assigned and can be increased.

In addition, here is what the standard language means:

5.1.2.2.1 Starting the program

...
2 If they are declared, the parameters of the main function must obey the following restrictions:
...
- Parameters argc and argv , and the lines pointed to by the argv array must be modified by the program and save their last saved values ​​between the program start and end the program.

EDIT

And here is the language for the function parameters:

6.7.6.3 Function declaration (including prototypes)

...
7. Declaring a parameter as an '' type array must be adjusted to a qualified pointer to a type where the quality type (if any) is that specified within [ and ] the array type. If the static also appears within [ and ] type of the array, then for each function call, the value of the corresponding actual argument must provide access to the first element of the array at least as many elements defined by the size expression.

EDIT 2

Some examples (assumes the C99 compiler):

 void foo(int a[], size_t len) { size_t i; printf("sizeof a = %zu\n", sizeof a); printf("sizeof (int *) = %zu\n", sizeof (int *)); for (i = 0; i < len; i++) printf("a[%zu] = %d\n", i, *a++); } int main(void) { int a1[5] = {0}; int a2[] = {0, 1, 2, 3, 4}; printf("sizeof a1 = %zu\n", sizeof a1); printf("sizeof a2 = %zu\n", sizeof a2); foo(a1, sizeof a1 / sizeof a1[0]); foo(a2, sizeof a2 / sizeof a2[0]); return 0; } 

Another part of the standard:

6.3.2.1 Lvalues, arrays, and function notation

...
3 _Alignof it is an operand of the sizeof operator, the _Alignof operator _Alignof or the unary operator & or the string literal used to initialize the array, an expression that is of type '' of type type is converted to an expression with type pointer '' to input such points to the source element an array object and is not an lvalue value. If the array object has a register storage class, the behavior is undefined.

In the main , a1 and a2 functions, they were declared as 5-element int arrays; a2 gets its size from the number of elements in the initializer. The expressions a1 and a2 , therefore, are of the types β€œ5-element array from int ”, and they may not be objects of the assignment expression, and also may not be operands of ++ or -- operators. When these expressions appear in a call to foo , their types are converted to a "pointer to int " according to the rule above. Thus, foo gets the value of the pointer, not the value of the array, for a (which is covered by a rule that says that the parameters of the array are converted to pointer types). Thus, the expression a in foo is of type int * or a pointer to int ; thus, a may be the target of the assignment, and it may be the operand of ++ and -- .

Another difference: according to the above rule, conversion to a pointer type does not occur when the array expression is an operand of the sizeof operator; sizeof a1 should estimate the number of bytes occupied by the a1 array (5 * sizeof int ). However, since a in foo is of type int * and not int [5] , sizeof a should only evaluate the number of bytes for a pointer to int (sizeof (int *) ).

+5
source

In your examples, array is a true array and therefore the non-modifiable value of l. Basically, since it is declared in the parameter list, argv is actually a char ** , that is, a pointer that can be changed.

It all comes down to the fact that char *array[] means different things, depending on the context.

+6
source
 main(int argc, char * argv[]) 

or

 main(int argc, char **argv) 

are the same and correct. Because in the argument argument, the arguments are unpacked into pointers

For more read this.

But the code you showed is an actual array. And the name of the array gives the address of the first element, and it is not modified, so for this:

  array = point; array++; 

wrong, as you mentioned.

+2
source

All Articles