任何人都可以解释为什么我在这里得到1吗?

问题描述 投票:0回答:2

这是指针的数学问题

    //code   
    #include<iostream>
    using namespace std;
    int main()   
    {
        int a=20,b=50,*p,*q;     
        p=&a;
        q=&b;
        int c=p-q;
        cout<<c;   //Here I'm getting 1 for all positive  value of 'a' and 'b'
        return 0;   
    }

我也尝试了long int c

c++ pointers memory-address pointer-arithmetic pointer-conversion
2个回答
1
投票

首先,您要减去指针,而不是取消引用的指针。如果希望通过指针从a中减去b的值,则需要使用*p*q


0
投票
int c = p - q;
© www.soinside.com 2019 - 2024. All rights reserved.