简体   繁体   中英

I am confused how to understand this code. contains double pointers

I don't understand why the code below changes the array b :

int a[] = { 3, 6, 9 };
int b[] = { 2, 4, 6, 8, 10 };
int **c;
int **d[2];
c = (int **)malloc (b[1] * sizeof(int *));
*c = &a[1];
c[1] = c[0] + 1;
*d = c;
c = c + 2;
*c = b;
c[1] = &c[0][3];
*(d + 1) = c;
d[0][3][1] = d[1][0][0];
d[1][0][2] = d[0][1][0];

I have run this code and found the values of array a and array b but I am unable to understand how these values come.

Array a remains unchanged while b becomes 2, 4, 9, 8, 2 . How does this happen?

c = (int**)malloc(b[1] * sizeof(int*));  //int **c[4] ???

c is an array of double pointers *c = &a[1] this means that c[0] has the address of array a's second index. I am not getting the way to interpret this.

The code contains actual statements, therefore it must be part of a function body, hence all declarations herein have automatic storage. It is highly convoluted, with purposely contrived double indirections... Lets analyse it one line at a time:

  • int a[] = { 3, 6, 9 }; -- a is an array of 3 ints initialized with some explicit values.

  • int b[] = { 2, 4, 6, 8, 10 }; -- likewise, b is an array of 3 ints initialized with some explicit values.

  • int **c; -- c is an uninitialized pointer to a pointer to int , that can be made to point to an array of pointers to int .

  • int **d[2]; -- d is an uninitialized array of 2 pointers to pointers to int , each of which can be made to point to an array of pointers to int .

  • c = (int **)malloc(b[1] * sizeof(int *)); -- c is set to point to a block of uninitialized memory with a size of 4 pointers to int . In short, c now points to an uninitialized array of 4 pointers to int .

  • *c = &a[1]; -- The element pointed to by c (aka A[0] ) is set to point to the second element of a (aka a[1] , with a value of 6 ). The value of A[0] is &a[1] .

  • c[1] = c[0] + 1; -- The second element in the array pointed to by c (aka A[1] ) is set to point to the element after the one pointed to by c[0] , hence it points to the third element of a (aka a[2] with a value of 9 ). The value of A[1] is &a[2]`.

  • *d = c; -- The first element of d is set to the value of pointer c , which is the address of A[0] . The value of d[0] is &A[0] .

  • c = c + 2; -- The pointer c is incremented by 2 , it now points to the third element of the array A allocated with malloc() , A[2] .

  • *c = b; -- The element pointed to by c , A[2] , which is itself a pointer, is set to point to the first element of b , b[0] . The value of A[2] is &b[0] .

  • c[1] = &c[0][3]; -- The element after that, A[3], the 4th element of the array allocated by malloc , is set to point to the 4th element of the array pointed to by the element c points to. &c[0][3] is equivalent to c[0] + 3 or &(*c)[3] or simply *c + 3 . This element is b[3] which has the value 8 . The value of A[3] is &b[3]`.

  • *(d + 1) = c; -- This is equivalent to d[1] = c; which sets the second element of d to the value of the pointer c , which is the address of the 3rd element of the array allocated wth malloc() , A[2] , which points to b[0] . The value of d[1] is &A[2] .

  • d[0][3][1] = d[1][0][0]; -- Let's rewrite these terms:

    • d[0][3][1] => (&A[0])[3][1] => A[3][1] => (&b[3])[1] => *((b + 3) + 1) => b[4]

    • d[1][0][0] => (&A[2])[0][0] => (*&A[2])[0] => A[2][0] => (&b[0])[0] => b[0] which is the value 2 .

    • Hence b[4] = 2; .

  • d[1][0][2] = d[0][1][0]; -- Let's rewrite these:

    • d[1][0][2] => (&A[2])[0][2] => (*&A[2])[2] => A[2][2] => (&b[0])[2] => (b + 0)[2] => b[2] .
    • d[0][1][0] => (&A[0])[1][0] , ie A[1][0] => (&a[2])[0] => *&a[2] => a[2] that has a value of 9 .
    • Hence b[2] = 9;

As a consequence, the array b now has elements { 2, 4, 9, 8, 2 } .

You can run the program:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int a[] = { 3, 6, 9 };
    int b[] = { 2, 4, 6, 8, 10 };
    int **c;
    int **d[2];
    c = (int **)malloc (b[1] * sizeof(int *));
    *c = &a[1];
    c[1] = c[0] + 1;
    *d = c;
    c = c + 2;
    *c = b;
    c[1] = &c[0][3];
    *(d + 1) = c;
    d[0][3][1] = d[1][0][0];
    d[1][0][2] = d[0][1][0];

    printf("a = { ");
    for (size_t i = 0; i < sizeof a / sizeof *a; i++)
        printf("%d, ", a[i]);
    printf("};\n");

    printf("b = { ");
    for (size_t i = 0; i < sizeof b / sizeof *b; i++)
        printf("%d, ", b[i]);
    printf("};\n");

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM