|
|
what is meant by the "equivalence of pointers and arrays" in C? - C
|
Views : 304
|
|
Tagged in : C
|
|
|
Report This Scrap as Inappropriate We request you to choose the appropriate categroy and subcategory that suits your
objectionable concern about the scrap, So that our team can review and find out whether it violates our Guidelines or the
scrap is not suitable for all viewers.
|
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)).
|
|
By Vijayaprasad, On - 2010-02-16 |
|
|
|