指针的地址

din*_*804 3 c++

我是编程新手.我有一个问题,我自己找不到一个可以理解的答案.我想通过使用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)

Gar*_*365 7

这条线显示地址 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)

  • p的类型为`char*`.您可能必须将其强制转换为`void*`,以便ostream不会将其解释为ac字符串 (7认同)