Basic knowledge points of C language (6) Two-dimensional array pointers and addresses

#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));
}
  1. printf("a:%p, a+1:%p\n", a, a + 1);

    Output result: a:0061FF08, a+1:0061FF14Explanation: aIt is a pointer to the first row of the two-dimensional array, and a+1it is a pointer to the second row of the two-dimensional array. Each row has 3 intelements, so the difference is 3 sizeof(int)bytes, which is 12 bytes.

  2. printf("&a:%p, &a+1:%p\n", &a, &a+1);

    Output result: &a:0061FF08, &a+1:0061FF20Explanation: &aIt is the address of the entire two-dimensional array, and &a+1it is the address behind the entire two-dimensional array. The entire two-dimensional array occupies 2 rows and 3 columns, with a total of 6 intelements, so the difference is 6 sizeof(int)bytes, which is 24 bytes.

  3. printf("a[0]:%p, a[0]+1:%p\n", a[0], a[0]+1);

    Output result: a[0]:0061FF08, a[0]+1:0061FF0CExplanation: a[0]It is the first row of the two-dimensional array and is a pointer to the first element. Each element takes up one sizeof(int)byte, so the difference is one sizeof(int)byte.

  4. printf("&a[0]:%p, &a:%p\n",&a[0], &a);

    Output result: &a[0]:0061FF08, &a:0061FF08Explanation: &a[0]It is the address of the first row of the two-dimensional array, which &ais the same as the pointer to the first row of the two-dimensional array.

  5. printf("&a[0]+1:%p, &a+1:%p\n",&a[0]+1, &a+1);

    Output result: &a[0]+1:0061FF14, &a+1:0061FF20Explanation: &a[0]+1One row is skipped, which is equivalent to pointing to the address of the second row; &a+1the entire two-dimensional array is skipped, which is equivalent to pointing to the address behind the entire two-dimensional array.

  6. printf("*a:%p, *a+1:%p,*(a+1):%p\n",*a, *a+1,*(a+1));

    Output result: *a:0061FF08, *a+1:0061FF0C,*(a+1):0061FF14Explanation: *aIt is a pointer to the first row. After dereference, the first element of the first row is obtained; it is *a+1offset by one sizeof(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.

  7. printf("**a:%p, **a+1:%p,**(a+1):%p\n",**a, **a+1,**(a+1));

    Output result: **a:00000002, **a+1:00000003,**(a+1):00000008Explanation: **aIt is a pointer to the first row. After dereferencing twice, the first element of the first row is obtained; **a+1add 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.

  8. 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):00000004Explanation: *a[0]It is the first element in the first line, and the value of the element is obtained after dereferencing; *a[0]+1plus 1; *(a[0]+1)it is the second element in the first line, and the value of the element is obtained after dereferencing.

  9. 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):0000000AExplanation: *a[1]It is the first element of the second line, and the value of the element is obtained after dereferencing; *a[1]+1plus 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:

  1. 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.

  2. 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.

  3. 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.

  4. Dereference operator: Use *the operator to dereference a pointer and obtain the value pointed to by the pointer.

  5. printf formatted output: %pused 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 ithe 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

Guess you like

Origin blog.csdn.net/qq_51519091/article/details/132911841