#include <stdio.h>
int main()
{
int a[2][3] = {
2, 4, 6,
8, 10, 12};
printf("a:%p, a+1:%p\n", a, a + 1); // 相差3*sizeof(int)=12,二维数组名是一个指向每一行的指针,a:0061FF08, a+1:0061FF14
printf("&a:%p, &a+1:%p\n", &a, &a + 1); // 相差6*sizeof(int)=1224,&a:0061FF08, &a+1:0061FF20
printf("a[0]:%p, a[0]+1:%p\n", a[0], a[0] + 1); // 相差一个sizeof(int)也就是一个数更多v组元素,a[0]:0061FF08, a[0]+1:0061FF0C
printf("&a[0]:%p, &a:%p\n", &a[0], &a); // 结果一样,&a[0]:0061FF08, &a:0061FF08
printf("&a[0]+1:%p, &a+1:%p\n", &a[0] + 1, &a + 1); //&a[0]跳了12个字节(跳过了一行),而&a跳了24个字节(跳过了整个二维数组)
printf("*a:%p, *a+1:%p,*(a+1):%p\n", *a, *a + 1, *(a + 1)); /* a:0061FF08, *a+1:0061FF0C,*(a+1):0061FF14
*a+1偏移了一个sizeof(int).*(a+1)偏移了一行 */
printf("**a:%p, **a+1:%p,**(a+1):%p\n", **a, **a + 1, **(a + 1));
printf("*a[0]:%p, *a[0]+1:%p,*(a[0]+1):%p\n", *a[0], *a[0] + 1, *(a[0] + 1));
printf("*a[1]:%p, *a[1]+1:%p,*(a[1]+1):%p\n", *a[1], *a[1] + 1, *(a[1] + 1));
}
-
printf("a:%p, a+1:%p\n", a, a + 1);
Output result:
a:0061FF08, a+1:0061FF14
Explanation:a
It is a pointer to the first row of the two-dimensional array, anda+1
it is a pointer to the second row of the two-dimensional array. Each row has 3int
elements, so the difference is 3sizeof(int)
bytes, which is 12 bytes. -
printf("&a:%p, &a+1:%p\n", &a, &a+1);
Output result:
&a:0061FF08, &a+1:0061FF20
Explanation:&a
It is the address of the entire two-dimensional array, and&a+1
it is the address behind the entire two-dimensional array. The entire two-dimensional array occupies 2 rows and 3 columns, with a total of 6int
elements, so the difference is 6sizeof(int)
bytes, which is 24 bytes. -
printf("a[0]:%p, a[0]+1:%p\n", a[0], a[0]+1);
Output result:
a[0]:0061FF08, a[0]+1:0061FF0C
Explanation:a[0]
It is the first row of the two-dimensional array and is a pointer to the first element. Each element takes up onesizeof(int)
byte, so the difference is onesizeof(int)
byte. -
printf("&a[0]:%p, &a:%p\n",&a[0], &a);
Output result:
&a[0]:0061FF08, &a:0061FF08
Explanation:&a[0]
It is the address of the first row of the two-dimensional array, which&a
is the same as the pointer to the first row of the two-dimensional array. -
printf("&a[0]+1:%p, &a+1:%p\n",&a[0]+1, &a+1);
Output result:
&a[0]+1:0061FF14, &a+1:0061FF20
Explanation:&a[0]+1
One row is skipped, which is equivalent to pointing to the address of the second row;&a+1
the entire two-dimensional array is skipped, which is equivalent to pointing to the address behind the entire two-dimensional array. -
printf("*a:%p, *a+1:%p,*(a+1):%p\n",*a, *a+1,*(a+1));
Output result:
*a:0061FF08, *a+1:0061FF0C,*(a+1):0061FF14
Explanation:*a
It is a pointer to the first row. After dereference, the first element of the first row is obtained; it is*a+1
offset by onesizeof(int)
byte, that is, it points to the second element of the first row;*(a+1)
it points to the second row. Pointer, after dereference, get the first element of the second row. -
printf("**a:%p, **a+1:%p,**(a+1):%p\n",**a, **a+1,**(a+1));
Output result:
**a:00000002, **a+1:00000003,**(a+1):00000008
Explanation:**a
It is a pointer to the first row. After dereferencing twice, the first element of the first row is obtained;**a+1
add 1 to the first element of the first row;**(a+1)
it is a pointer to the second row, which is dereferenced. After two times we get the first element of the second row. -
printf("*a[0]:%p, *a[0]+1:%p,*(a[0]+1):%p\n",*a[0], *a[0]+1,*(a[0]+1));
Output result:
*a[0]:00000002, *a[0]+1:00000003,*(a[0]+1):00000004
Explanation:*a[0]
It is the first element in the first line, and the value of the element is obtained after dereferencing;*a[0]+1
plus 1;*(a[0]+1)
it is the second element in the first line, and the value of the element is obtained after dereferencing. -
printf("*a[1]:%p, *a[1]+1:%p,*(a[1]+1):%p\n",*a[1], *a[1]+1,*(a[1]+1));
Output result:
*a[1]:00000008, *a[1]+1:00000009,*(a[1]+1):0000000A
Explanation:*a[1]
It is the first element of the second line, and the value of the element is obtained after dereferencing;*a[1]+1
plus 1;*(a[1]+1)
it is the second element of the second line, and the value of the element is obtained after dereferencing.
Involving related knowledge points of pointers and arrays, the following are some summaries:
-
Pointers and addresses: Pointers are variables that store memory addresses. Address operations can be performed by adding/subtracting integers.
&
Operator is used to get the address of a variable. -
Arrays and Pointers: An array name can be thought of as a pointer to the first element of the array. For a two-dimensional array, each row can be thought of as a pointer to the first element of the row. Use
a[i]
or*(a+i)
to access array elements. -
Pointer arithmetic: The result of pointer arithmetic depends on the pointer type and the type of the operand. When adding/subtracting a pointer, the corresponding offset will be performed based on the size of the type pointed to by the pointer.
-
Dereference operator: Use
*
the operator to dereference a pointer and obtain the value pointed to by the pointer. -
printf formatted output:
%p
used to print the value of the pointer address.
The two-dimensional array name points to the row , " &two-dimensional array name " points to the entire two-dimensional array , and "two-dimensional array name[i] " points to the address of the first element of row i . " &two-dimensional array name[i] " points to a row .
In C language, the array name is a pointer to the first element of the array, that is, it itself represents the address of the first element of the array. Therefore, when using an array name, you can think of it as a pointer to the first element of the array, for example, a pointer to the first element of the row of a[i]
a two-dimensional array.i
For a two-dimensional array, &a[i]
it represents i
the address of the row of the two-dimensional array. However, since the array name already represents the address of the first element of the array, they are actually equivalent a[i]
and &a[i]
the values they represent are also the same. .
*The two-dimensional array name [i] is to take out the value of the first element of the i-th row,
*The two-dimensional array name [i]+1 is to +1 the value of the first element of the i-th row,
*(two-dimensional array name [i]+1) is to +1 the value of the first element taken out of the i+1th row