代码之家  ›  专栏  ›  技术社区  ›  Homer Jay Simpson

C中的指针解释++

  •  -2
  • Homer Jay Simpson  · 技术社区  · 3 年前

    我正在自学C++,我在C++的Pointers部分找到了自己。

    据我所知,指针将变量的值分配给内存。

    但我遇到了这个问题,答案是588。

    我不知道这个数字是怎么来的。

    有人能一步一步地向我解释588是如何产生的吗?

    提前谢谢。

    
    #include <iostream>
    
    int main() {
        int *p, *q, *t;
    
        // Allocate memory and initialize pointers and values
        p = new int;
        q = new int;
        t = new int;
        *p = 17;
        *q = 7;
        *t = 42;
    
        p = t; // Make pointer p point to the same location as pointer t
        t = q; // Make pointer t point to the same location as pointer q
    
        *t = *p * *q;
    
        *q = *q + *t;
    
        std::cout << *t << std::endl;
    
    
        return 0;
    }
    
    
    3 回复  |  直到 3 年前
        1
  •  2
  •   Joel    3 年前

    如果在每次更改后输出所有三个指针及其值,或者使用调试器,您可以很容易地了解发生了什么。发生的情况如下:

    *p = 17;
    *q = 7;
    *t = 42;
    
    // p points to an int with value 17
    // q points to an int with value 7
    // t points to an int with value 42
    
    p = t; // Make pointer p point to the same location as pointer t
    
    // p points to an int with value 42 (the same as t)
    // q points to an int with value 7
    // t points to an int with value 42 (the same as p)
        
    t = q; // Make pointer t point to the same location as pointer q
    
    // p points to an int with value 42 (now different than t)
    // q points to an int with value 7 (the same as t)
    // t points to an int with value 7 (the same as q)
    
    *t = *p * *q;   // *t = (*p) * (*q) = 42 * 7 = 294
    
    // p points to an int with value 42
    // q points to an int with value 294 (still the same as t)
    // t points to an int with value 294 (still the same as q)
    
    *q = *q + *t;   // *q = (*q) + (*t) = 294 + 294 = 588
    
    // p points to an int with value 42
    // q points to an int with value 588 (still the same as t)
    // t points to an int with value 588 (still the same as q)
    
    std::cout << *t << std::endl;   // prints 588 (the same as q)
    

    但是请注意 p = t 覆盖地址 p 指向,因此您无法再释放分配的内存。

        2
  •  1
  •   Vlad from Moscow    3 年前

    程序的评论中有

    p = t; // Make pointer p point to the same location as pointer t
    t = q; // Make pointer t point to the same location as pointer q
    

    那是在这些陈述之后 *p 等于 42 和 *t 等于 7 与 *q 等于 7. 因为现在两个指针 t 和 q 指向相同的内存。

    因此,你有了这个声明

    *t = *p * *q;
    

    对

    *t = 42 * 7;
    

    那是指针指向的对象 t 和 q 现在包含 294 。

    本声明

    *q = *q + *t;
    

    这和

    *t = *q + *t;
    

    因为这两个指针 t 和 q 指向同一对象,也与

    *t = *t + *t;
    

    或

    *q = *q + *q;
    

    所以 294 plust 294 yelds 588 。

        3
  •  1
  •   Remy Lebeau    3 年前

    如果有疑问,你应该把它拔出来!例如

    int *p, *q, *t;
    

    您声明的3个指针还没有指向任何位置,因此:

    p -> ?
    q -> ?
    t -> ?
    
    p = new int;
    q = new int;
    t = new int;
    

    您正在分配3个整数(初始值不确定),并使3个指针指向它们,因此:

    p -> [ ... ]
    q -> [ ... ]
    t -> [ ... ]
    
    *p = 17;
    *q = 7;
    *t = 42;
    

    您正在取消引用指针,并设置它们所指向的整数的值,因此:

    p -> [ 17 ]
    q -> [ 7  ]
    t -> [ 42 ]
    
    p = t;
    

    你正在制造 p 指向与 t 指向,因此:

    p -+   [ 17 ]
       |
    q -|-> [ 7  ]
       |
    t -+-> [ 42 ]
    

    t=q;

    你正在制造 t 指向与 q 指向,因此:

    p -+     [ 17 ]
       |
    q -|-+-> [ 7  ]
       | |
    t -|-+
       |
       +---> [ 42 ]
    
    *t = *p * *q;
    

    您正在乘以 p 和 q 指向(42*7),并将结果(294)分配给 t 指向,因此:

    p -+     [ 17 ]
       |
    q -|-+-> [ 294 ]
       | |
    t -|-+
       |
       +---> [ 42 ]
    
    *q = *q + *t;
    

    您正在将 q 和 t 都指向(294+294),并将结果(588)分配给 q 指向,因此:

    p -+     [ 17 ]
       |
    q -|-+-> [ 588 ]
       | |
    t -|-+
       |
       +---> [ 42 ]
    
    std::cout << *t << std::endl;
    

    您正在打印出 t 指向,因此:

    588