我是编程新手.我有一个问题,我自己找不到一个可以理解的答案.我想通过使用C++和C找到指针的地址,但是两个结果是不同的,尽管它们有一些相似的数字.他们还是同一个地址吗?
adress of g is :0018F9C4
address of g is: 0018F9D3
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
#include<iostream>
#include<stdio.h>
void main()
{
char g = 'z';
char*p;
p = &g;
std::cout << "adress of g is :" << &p;
printf("\naddress of g is: %p", p);
}
Run Code Online (Sandbox Code Playgroud)
这条线显示地址的 p
std::cout << "address of p is :" << &p;
Run Code Online (Sandbox Code Playgroud)
这条线显示地址中 p,即,地址的 g
printf("\naddress of g is: %p", p);
Run Code Online (Sandbox Code Playgroud)
有不同的结果是正常的.
尝试
std::cout << "address of g is :" << static_cast<void*>(p);
printf("\naddress of g is: %p", p);
Run Code Online (Sandbox Code Playgroud)