这是指针的数学问题
//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
首先,您要减去指针,而不是取消引用的指针。如果希望通过指针从a
中减去b
的值,则需要使用*p
和*q
。
int c = p - q;