K
Keith Thompson
arnuld said:okay, I got it. Arrays and pointers are different things with two
exceptions:
Close, but arrays and pointers are different things, period. (Or
"full stop" if you prefer.)
1) passing an array as an argument to a function. In that case, you never
access the array but the pointer to the first element and then you use
pointer arithmetic to access the array elements.
2) when a pointer is assigned the value of an array (even when we are not
talking about function arguments) then we can access the array using
pointer arithmetic. In fact, C, implicitly converts a into p + i where
p &a[0], C always index into array using pointers.
1 and 2 are just special cases of a more general rule.
Any expression of array type (which could, for example, be the name of
an array object) is implicitly converted to a pointer to the array's
first element *unless* the array expression is the operand of a unary
"sizeof" or "&" operator, or is a string literal used to initialize an
array.
An array object name appearing as a function argument, or on the right
hand side of an assignment, or in an initializer, as long as it's not
one of the three cases I mentioned, is converted to a pointer to the
first element. So in func(array_obj), array_obj isn't converted to a
pointer because it's being passed to a function; it's converted to a
pointer because it's an expression of array type.
The [] operator always takes a pointer operand and an integer operand;
very often the pointer operand is the result of implicitly converting
an array expression.
And one more piece of the puzzle: If a function parameter is declared
as an array:
void foo(int param[]);
it's really a pointer, not an array. There's no conversion here; in
this context, "int param[]" *means* "int *param".