7

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?

Liao Pengyu
  • 581
  • 3
  • 11
  • I had no idea array parameters were degraded to pointers this way. Thanks for asking the question that brought it up! – Lstor Nov 21 '12 at 23:03

3 Answers3

6

In your examples array is a true array, and thus a non-modifiable l-value. In main, since it's declared in the parameter list, argv is actually a char **, i.e. a pointer which is modifiable.

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

cnicutar
  • 164,886
  • 23
  • 329
  • 361
5

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

(edit -- note that this is true only for function parameter declarations; for a regular variable declaration, T a[N] and T a[] both declare a as an array of T).

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:

5.1.2.2.1 Program startup

...
2 If they are declared, the parameters to the main function shall obey the following constraints:
...
— The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

EDIT

And here's the language for function parameters:

6.7.6.3 Function declarators (including prototypes)

...
7 A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation. If the keyword static also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression.

EDIT2

Some examples (assumes a 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;
}

One more piece of standardese:

6.3.2.1 Lvalues, arrays, and function designators

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

In the function main, a1 and a2 have been declared as 5-element arrays of int; a2 gets its size from the number of elements in the initializer. The expressions a1 and a2 thus have types "5-element array of int" 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 to foo, their types are converted to "pointer to int" per the rule above. Thus foo receives a pointer value, not an array value, for a (which is covered by the rule that says array parameters are converted to pointer types). So the expression a in foo has type int *, or pointer to int; thus, a may 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 sizeof operator; sizeof a1 should evaluate to the number of bytes taken up by the array a1 (5 * sizeof int). However, since a in foo has type int *, not int [5], sizeof a should only evaluate to the number of bytes for an pointer to int (sizeof (int *)).

John Bode
  • 106,204
  • 16
  • 103
  • 178
2
main(int argc, char * argv[])

or

main(int argc, char **argv)

are same and correct .Because in function arguments array are decayed into pointers

For more read this

But the code you have shown is the actual array . And name of the array gives the address of the first element and it's non modifiable that's why doing this :

 array = point; 
array++;

is wrong as you have already mentioned it.

Community
  • 1
  • 1
Omkant
  • 8,362
  • 7
  • 36
  • 57
  • Decaying of array explains. BTW, The `void by_reference(const T (&array)[U])` in the url you given is said can give proper sizeof() info, but when i test it in my code, I got a error. Why? – Liao Pengyu Nov 21 '12 at 20:20
  • It's the pass by reference which is in C++ not in C.And remember reference is just an alias of the same variable. – Omkant Nov 21 '12 at 20:24