what is meant by the "equivalence of pointers and arrays" in C?

by Vijayaprasad 2010-02-16 10:05:39

Much of the confusion surrounding pointers in C can be traced to a misunderstanding of this statement. Saying that arrays and pointers are "equivalent" does not by any means imply that they are interchangeable.

"Equivalence" refers to the following key definition:
An lvalue of type array-of-T which appears in an expression decays (with three exceptions) into a pointer to its first element; the type of the resultant pointer is pointer-to-T.

(The exceptions are when the array is the operand of the sizeof() operator or of the & operator, or is a literal string initializer for a character array.)

As a consequence of this definition, there is not really any difference in the behavior of the "array subscripting" operator [] as it applies to arrays and pointers. In an expression of the form a[i], the array reference "a" decays into a pointer, following the rule above, and is then subscripted exactly as would be a pointer variable in the expression p[i]. In either case, the expression
x[i] (where x is an array or a pointer) is, by definition, exactly equivalent to *((x)+(i)).

Tagged in:

1312
like
0
dislike
0
mail
flag

You must LOGIN to add comments