a a[0] &a &a[0]的理解

An array of several key symbol (aa [0] & a & a [0]) understanding (provided that the A int [10])
(1) This four symbols to figure out a lot of problems associated with the array has the answer. Time to understand these symbols and values of the left and right combination of value, that is, to figure out the meaning of each symbol are doing different when the left and right values.
(2) a is the array name. a represents a whole array of all the space (10 × 4 = 40 bytes) left value do, and because C language provides for independent operation of a single array operation, not the overall operation of the array, so that a value can not be left to do; do a rvalue an array of the first element (element 0 of the array, i.e. a [0]) of the first address (the first address is the starting address is four bytes beginning address of the first byte). a value equivalent to the right to make a & [0];
(2) a [0] indicates the first element in the array, i.e. 0-th element of the array. An array element corresponding to the 0-th memory space (four consecutive bytes) do a left value; represents a value of 0 element in the array do rvalue (i.e. the number of the array element corresponding to 0 is stored in memory )
(3) & a is the array name a prefetch address, literal meaning should be the address of the array. do not be left & a value (& a is essentially a constant, not a variable and therefore can not be assigned, so do not be left natural value.); & a whole array represents the first address when to do the right value.
(4) & a [0] is literally the first address of the array element 0 (clear [] & priorities and, [] is higher priority than &, and therefore a first [] and then take the address binding) . The first element of the array represents the memory space corresponding to the time left to do value represents the value (that is, the value of the memory space corresponding to the first element of the array is stored) The first element of the array do the right value. Do rvalue & a [0] is equivalent to a.

Explanation: Why is the address of the array is constant? Because arrays is that the compiler automatically allocated in memory. When every time we execute the program, the runtime will help us allocate a block of memory to the array, as long as the completion of the assignment, the address of this array would be good, and this program runs until the termination can not be changed again. So we have in the program only through & to obtain a distribution of the address, but not to modify it using the assignment operator.

to sum up:
1: & a and make a difference when the right values: & a is the first address of the entire array, and a is the first address of the first element of the array. The two numerically equal, but not identical meaning. Meaning is not the same as cause them to behave differently when involved in computing.
2: a and & a [0] to do exactly the same meaning and the right numerical values, can replace each other.
3: & a is a constant, not an lvalue.

4: a lvalue is representative of the array of all the space, so a value can not be left to do.

 

(1) int * p; int a [5]; p = a; // matching type
(1) int * p; int a [5]; p = & a; // type mismatch. p is int *, & a is a pointer to an entire array, i.e., an array pointer type, not int pointer type, it does not match
(2) & a, a, & a [0] from the numerical point of view is completely equal, but the sense look different.

In the sense point of view, a and & a [0] is the first address of the first element of the array, and & a is the first address of the entire array;

From the type of view, a and & a [0] is a pointer element, which is of type int *; and & a is an array of pointers, is int (*) [5]; type.

Guess you like

Origin www.cnblogs.com/fengliu-/p/10960387.html