In C/C ++, address offsets in one-dimensional arrays and two-dimensional arrays are discussed.
One-dimensional array int a [3];
Two-dimensional array int a [3] [3];
1. first look at the situation of one-dimensional arrays:
[Cpp]
# Include <iostream>
Using namespace std;
Int main ()
{
Int a [3] = {1, 2, 3 };
Cout <& a <endl;
Cout <a <endl;
Cout <& a [0] <endl;
Cout <a [0] <endl;
Cout <& a + 1 <endl;
Cout <a + 1 <endl;
Cout <& a [0] + 1 <endl;
Cout <a [0] + 1 <endl;
System ("pause ");
Return 0;
}
# Include <iostream>
Using namespace std;
Int main ()
{
Int a [3] = {1, 2, 3 };
Cout <& a <endl;
Cout <a <endl;
Cout <& a [0] <endl;
Cout <a [0] <endl;
Cout <& a + 1 <endl;
Cout <a + 1 <endl;
Cout <& a [0] + 1 <endl;
Cout <a [0] + 1 <endl;
System ("pause ");
Return 0;
}
The result shows that & a, a, and & a [0] indicate the same address, but the level is different.
The & a + 1 Address and & a offset is 12 bytes, indicating the space size of the array;
The a + 1 address is 4 bytes different from the address, that is, the space size of an element in the array;
The address & a [0] + 1 is 4 bytes offset from the address & a [0], that is, the space size of an element in the array;
That is to say, & a, a, and & a [0] indicate the same address, but the compiler will differentiate them. & a points to the address of the entire array, which is the most advanced address in the array, a and & a [0] indicate that & a represents the next level of address.
2. Two-dimensional array
[Cpp]
# Include <iostream>
Using namespace std;
Int main ()
{
Int a [3] [3] = {1, 2, 3}, {4, 5, 6}, {7, 8, 9 }};
Cout <& a <endl;
Cout <a <endl;
Cout <& a [0] <endl;
Cout <a [0] <endl;
Cout <& a [0] [0] <endl;
Cout <a [0] [0] <endl;
Cout <& a + 1 <endl;
Cout <a + 1 <endl;
Cout <& a [0] + 1 <endl;
Cout <a [0] + 1 <endl;
Cout <& a [0] [0] + 1 <endl;
Cout <a [0] [0] + 1 <endl;
System ("pause ");
Return 0;
}
# Include <iostream>
Using namespace std;
Int main ()
{
Int a [3] [3] = {1, 2, 3}, {4, 5, 6}, {7, 8, 9 }};
Cout <& a <endl;
Cout <a <endl;
Cout <& a [0] <endl;
Cout <a [0] <endl;
Cout <& a [0] [0] <endl;
Cout <a [0] [0] <endl;
Cout <& a + 1 <endl;
Cout <a + 1 <endl;
Cout <& a [0] + 1 <endl;
Cout <a [0] + 1 <endl;
Cout <& a [0] [0] + 1 <endl;
Cout <a [0] [0] + 1 <endl;
System ("pause ");
Return 0;
}
Result Analysis:
& A, a, & a [0], a [0], and & a [0] [0] indicate the same address, but the level difference is large.
The declared int a [3] [3] is 36 bytes in size.
The & a + 1 Address and & a offset is 36 bytes, indicating the space size of the array;
The a + 1 address is a-byte offset of 12 bytes, that is, the space size of a row of elements in the array;
The address & a [0] + 1 is offset by 12 bytes compared with & a [0], that is, the space size of a row of elements in the array;
The address a [0] + 1 is 4 bytes different from the address a [0], that is, the space size of an element in the array;
& A [0] [0] + 1 address is 4 bytes offset compared with & a [0] [0], that is, the space size of an element in the array;
As can be seen from the above, & a is the highest level of address, and a + 1 and & a [0] + 1 are the second level of address, a [0] + 1 and & a [0] [0] + 1 represent the third-level address in a two-dimensional array.