有人能给我解释一下双指针是怎么工作的吗?例如,在这段代码中,为什么它最终会打印出它打印的值?

int main(void) {
    int anArray[] = {5,16,33,99};
    int * p = anArray;

    printf("*p = %d\n", *p);
    p++;
    printf("Now *p = %d\n", *p);

    int * q = &anArray[3];
    int ** x = &q;
    **x = 12;
    *x = p;
    **x = 42;
    q[1] = 9;

    for (int i =0; i < 4; i++){
        printf("anArray[%d] = %d\n",i, anArray[i]);
    }

    return EXIT_SUCCESS;
}

推荐答案

让我们把它完全画出来:

int anArray[] = {5,16,33,99};  // 4 values in consecutive addresses

//            ----
// anArray ->   5
//            ----
//             16
//            ----
//             33
//            ----
//             99
//            ----

int * p = anArray; // A new variable, whose contents points to the address of anArray

//            ---------
// anArray ->   5       <-+
//            ---------   |
//             16         |
//            ---------   |
//             33         |
//            ---------   |
//             99         |
//            ---------   |
//       p ->  anArray   -+
//            ---------

printf("*p = %d\n", *p);  // prints 5
p++;                      // increment pointer p to the next element.

//            -----------
// anArray ->   5      
//            -----------
//             16         <-+
//            -----------   |
//             33           |
//            -----------   |
//             99           |
//            -----------   |
//       p ->  anArray+1   -+
//            ---------

printf("Now *p = %d\n", *p); // prints 16
int * q = &anArray[3];       // new pointer variable, points to 4th element of anArray (0-based)

//            -----------
// anArray ->   5      
//            -----------
//             16         <-+
//            -----------   |
//             33           |
//            -----------   |
//             99           | <-+
//            -----------   |   |
//       p ->  anArray+1   -+   |
//            -----------       |
//       q ->  anArray+3   -----+
//            -----------

int ** x = &q;  // new pointer variable, points to address of q pointer variable

//            -----------
// anArray ->   5      
//            -----------
//             16         <-+
//            -----------   |
//             33           |
//            -----------   |
//             99           | <-+
//            -----------   |   |
//       p ->  anArray+1   -+   |
//            -----------       |
//       q ->  anArray+3   -----+ <-+
//            -----------           |
//       x ->  q           ---------+
//            -----------

**x = 12;  // get address in x (q), then address in q (anArray+3) and write 12 to it

//            -----------
// anArray ->   5      
//            -----------
//             16         <-+
//            -----------   |
//             33           |
//            -----------   |
//             12           | <-+
//            -----------   |   |
//       p ->  anArray+1   -+   |
//            -----------       |
//       q ->  anArray+3   -----+ <-+
//            -----------           |
//       x ->  q           ---------+
//            -----------

*x = p;  // get address in x (q) and write p value to it (anArray+1)

//            -----------
// anArray ->   5      
//            -----------
//             16         <-+ <-+
//            -----------   |   |
//             33           |   |
//            -----------   |   |
//             12           |   |
//            -----------   |   |
//       p ->  anArray+1   -+   |
//            -----------       |
//       q ->  anArray+1   -----+ <-+
//            -----------           |
//       x ->  q           ---------+
//            -----------

**x = 42;  // get address in x (q), then address in q (anArray+1) and write a 42 to it

//            -----------
// anArray ->   5
//            -----------
//             42         <-+ <-+
//            -----------   |   |
//             33           |   |
//            -----------   |   |
//             12           |   |
//            -----------   |   |
//       p ->  anArray+1   -+   |
//            -----------       |
//       q ->  anArray+1   -----+ <-+
//            -----------           |
//       x ->  q           ---------+
//            -----------

q[1] = 9;  // get address in q (anArray+1) add one element (anArray+2), and assign 9

//            -----------
// anArray ->   5      
//            -----------
//             42         <-+ <-+   (q[0])
//            -----------   |   |
//              9           |   |   (q[1])
//            -----------   |   |
//             12           |   |
//            -----------   |   |
//       p ->  anArray+1   -+   |
//            -----------       |
//       q ->  anArray+1   -----+ <-+
//            -----------           |
//       x ->  q           ---------+
//            -----------

C++相关问答推荐

如何从TPS特定的TGPT_PUBLIC数据 struct 中以OpenSSL的EVP_PKEY

带双指针的2D数组

如何通过Zephyr(Devicetree)在PR Pico上设置UTE 1?

C strlen on char array

无法用C++编译我的单元测试

在C语言中,是否可以使枚举数向后计数?

GTK函数调用将完全不相关的char* 值搞乱

为什么函数是按照定义的顺序执行的,而不是按照从avr-c中的int main()调用的顺序执行的?

如何使用libgpio(d)为Raspberry Pi编译C程序?

C语言中的外部关键字

静态初始化顺序失败是否适用于C语言?

如何使用空元素块声明指针数组

即使我在C++中空闲,也肯定会丢失内存

将某些内容添加到链接列表时,列表中的其他项将使用最后添加的项的名称

生成的头文件不包括用户定义的文件

如何解释数组中的*(ptr)和*(ptr+2)?

如何打印循环调度问题的时间表

从文件到链表读取日期

C中2个数字的加法 - 简单的人类方法

无法理解 fgets 输出