c - %p 是将变量的地址打印为指针,还是打印它指向的内容?

标签 c pointers printf

我现在已经阅读了至少十几篇关于 %p 的文章,但我仍然没有得到明确的答案。我意识到网站上有很多关于此的帖子,并且我阅读了很多。 I even read this article, but it did not explain it adequately

假设我有:

int x = 10;
void *p = &x;

下面的语句是打印出'p'的地址还是'x'的地址?

printf( "Here: %p\n" , p );

我正在尝试通过 p 获取 x 的地址。


对于某些上下文,我正在遍历堆栈以查找指向我创建的链表的指针:

void foo(){

    register void* base asm("ebp");
    void* iter = base;

    void* mBase = MAIN_BASE; //global defined as "register void* base asm("ebp")" in main function

    void* start = &head;
    void* fin = &tail + sizeof( tail );

    while( iter != mBase ){

        if( iter >= start && iter <= fin )
            fprintf( stdout, ">>>>%p\n", iter );

        printf("iter: %p\n", iter );
        iter = (void*)( (char*)iter + 1 );
    }
}

最佳答案

打印变量p的内容,正好是x的地址,所以打印x的地址。它使用特定体系结构的地址格式显示它。格式可能因架构而异:存储在远段数据中的变量可以使用生成 MS DOS 实模式可执行文件的 C 编译器以完全不同的格式打印,例如 025A:0008 或类似的东西。

关于c - %p 是将变量的地址打印为指针,还是打印它指向的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20318976/

相关文章:

c - 从函数接收数组指针

c++ - 在 C++ 中,对于某些类型 T,如何从 "const T**"获取 "T**"?

c - C 中的输入文件双指针

c - printf() 的行为

c++ - 模板化 snprintf

c - c 中的 scanf 和换行符

c - 将空字符插入 snprintf

我不能只打印一个循环吗? - C

c - 警告 - Expected ‘struct node **’ but argument is of type ‘struct node **’ 是什么意思?

C - 将字符显示为十六进制