资治通鉴 发表于 2020-10-23 13:32:25

如果两个指针变量指向同一个变量,那么这两个指针的地址则一定相同吗?

#include <iostream>
using namespace std;

int main(void)
{
      int a = 1;
      int b = 1;
      int *pa = &a;
      int *pb = pa;
      int *pc = &a;
      int *pd = &b;
      printf("Address of pa is %p, and address of what pa points is %p\n", pa, &(*pa));
      printf("Address of pb is %p, and address of what pb points is %p\n", pb, &(*pb));
      printf("Address of pc is %p, and address of what pc points is %p\n", pc, &(*pc));
      printf("Address of pd is %p, and address of what pd points is %p\n", pd, &(*pd));
      return 0;
}


输出:

Address of pa: 0x7fff8bada600, address of what pa points: 0x7fff8bada600
Address of pb: 0x7fff8bada600, address of what pb points: 0x7fff8bada600
Address of pc: 0x7fff8bada600, address of what pc points: 0x7fff8bada600
Address of pd: 0x7fff8bada604, address of what pd points: 0x7fff8bada604
页: [1]
查看完整版本: 如果两个指针变量指向同一个变量,那么这两个指针的地址则一定相同吗?