C language: array Name Description details, array meaning details
1. One-dimensional array int a [5];
A: The array name. When a is left, it indicates all the space of the entire array (10 × 4 = 40 bytes). Because the C language specifies that an array operation must be performed independently, the array cannot be operated as a whole, therefore, a cannot make the left value. a does the right value to indicate the first address (the first address is the start address) of the first element of the array (0th elements of the array, that is, a [0, is the address of the first byte in the four bytes ). A's right value is equivalent to & a [0];
A [0]: indicates the first element of the array, that is, the 0th elements of the array. When the left value is used, the memory space corresponding to the 0th elements of the array (4 bytes in a row) is displayed ); when the right value is set, it indicates the value of the array's 0th elements (that is, the number of elements stored in the memory space corresponding to the array's 0th elements)
& A: the address of array name a, which literally means the address of the array. & A cannot be a left value (& a is essentially a constant. It is not a variable, so it cannot be a value, so it cannot be a left value .); & A indicates the first address of the entire array when the right value is set.
& A [0]: literally, it means the first address of the array's 0th elements (to clarify the priority of [] and &, the priority of [] is higher &, so a first works with [] And then obtains the address ). The left value indicates the memory space corresponding to the first element of the array, and the right value indicates the value of the first element of the array (that is, the value stored in the memory space corresponding to the first element of the array ). When the right value is set, & a [0] is equivalent to.
Summary:
1: What is the difference between & a and a when doing the right value: & a is the first address of the entire array, and a is the first address of the first element of the array.
The two are equal in numbers, but their meanings are different. Different meanings may lead to different performance during calculation.
2: When a and & a [0] are used as the right value, their meanings and values are exactly the same, and they can be replaced by each other.
3: & a is a constant and cannot be left.
4: The left value of a indicates all spaces in the entire array. Therefore, a cannot be the left value.
# Include
Int main (void)
{
Int a [5] = {1, 2, 3, 4, 5 };
Int * p1;
Int (* p2) [5];
Printf ("p1 = % p \ n", p1 );
Printf ("p2 = % p \ n", p2 );
P1 =;
P2 = &;
Printf ("p1 = % p \ n", p1 );
Printf ("(p1 + 1) = % p \ n", (p1 + 1 ));
Printf ("p2 = % p \ n", p2 );
Printf ("(p2 + 1) = % p \ n", (p2 + 1 ));
Return 0;
}
/***** Running result
P1 = 0xbfbcda54
P2 = 0xbfbcda5c
P1 = 0xbfbcd99c
(P1 + 1) = 0xbfbcd9a0
P2 = 0xbfbcd99c
(P2 + 1) = 0xbfbcd9b0
*******************/
/********* Analysis :*****
1: p1 p2 is not initialized when it is defined, so it is a wild pointer.
2: p1 = a. the right value of array a indicates the first address of the first element, while array a is of the int type, therefore, the first element's first address also contains the int type. That is to say, p1 is a pointer to the number of int types.
3: p1 + 1 = p1 + 4 p1 points to the first element address of the array, equivalent to p1 points to the inside of the array, so p1 + 1 is actually p1 + sizeof (array type)
4: p2 = & a p2 is defined as an int (* p2) [5] pointer pointing to the int [5] type, so p2 is a pointer to an array.
& A indicates the address of the array name, indicating the address of the array (indicating that the address stores an array type), so p2 matches the type of &.
5: p2 + 1 = p2 + 20 because p2 is defined to point to an int [5] address
P2 + 1 = p2 + sizeof (int [5]);
6: pointer + 1 is actually pointer + siezof (pointer type). This is actually determined at the time of definition, because during initialization
The Type pointed to by the pointer must match the type defined by the pointer.
For example
Char a [5];
Int * p; // p is a pointer to the int type.
P = a; the type does not match and compilation fails. However, if the initialization succeeds, the pointer points to an error when the operation is performed again.
For example, if the address of p = a is 0xb2000000, then p + 1 is 0xb2000004, and 0xb2000004 is actually a [3] instead of a [1 ],
The computation fails. Therefore, pointer type matching is mainly used for computation.
**************************************** ***/
2. Two-dimensional array
# Include
Int main (void)
{
Int a [2] [5];
Int * p1;
Int (* p2) [5];
Int B = 5;
// P1 = a; // compilation error, Type Mismatch
P2 = a; // No compilation error, type match
Printf ("a = % p \ n", );
Printf ("& a [0] = % p \ n", & a [0]);
Printf ("p2 = % p \ n", p2 );
Printf ("p2 + 1 = % p \ n", p2 + 1); // The p2 + 1 and p2 values remain unchanged.
Printf ("p2 = % p \ n", p2 );
Printf ("* (p2 + 1) + 1 = % p \ n", * (p2 + 1) + 1 );
Return 0;
}
************
A = 0xbfeaebc8
& A [0] = 0xbfeaebc8
P2 = 0xbfeaebc8
P2 + 1 = 0 xbfeaebdc
* (P2 + 1) + 1 = 0xbfeaebe0
**************************/
/************ Analysis *****
1: p2 is an int * [5] type pointer to int [5,
2: The array name uses the right value to indicate the first address of the array element. The array name of the Two-dimensional array represents the first-dimensional address, and the type is also int [5].
So it matches the p2 type.
3: p2 + 1 = p2 + 20, that is, p2 points to int [] [5], so p2 + 1 actually points to a [1] [], that is, the second element of the first dimension.
4: * (p2 + 1) + 1 = (p1 + 1) + 4, pointing to a [1] [1] That is, * (p + I) + j) equal to a [I] [j].
*/