【问题标题】:How to print variable addresses in C?如何在C中打印变量地址?
【发布时间】:2011-03-12 23:56:37
【问题描述】:

当我运行这段代码时。

#include <stdio.h>

void moo(int a, int *b);

int main()
{
    int x;
    int *y;

    x = 1;
    y = &x;

    printf("Address of x = %d, value of x = %d\n", &x, x);
    printf("Address of y = &d, value of y = %d, value of *y = %d\n", &y, y, *y);
    moo(9, y);
}

void moo(int a, int *b)
{
    printf("Address of a = %d, value of a = %d\n", &a, a);
    printf("Address of b = %d, value of b = %d, value of *b = %d\n", &b, b, *b);
}

我的编译器中不断出现这个错误。

/Volumes/MY USB/C Programming/Practice/addresses.c:16: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c:17: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
/Volumes/MY USB/C Programming/Practice/addresses.c:17: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c: In function ‘moo’:
/Volumes/MY USB/C Programming/Practice/addresses.c:23: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c:24: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
/Volumes/MY USB/C Programming/Practice/addresses.c:24: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’

你能帮帮我吗?

谢谢

布拉格曼

【问题讨论】:

  • 那些说“警告”,而不是“错误”。这意味着您的程序仍将运行。但请按照以下答案修复警告。

标签: c pointers memory-address


【解决方案1】:

您想使用%p 打印指针。来自规范:

p 参数应为指向 void 的指针。指针的值以实现定义的方式转换为打印字符序列。

别忘了演员阵容,例如

printf("%p\n",(void*)&a);

【讨论】:

  • 这就是我最初的想法,但是这个网站上的教程 (cis.temple.edu/~ingargio/cis71/code/addresses.c) 告诉我使用 %d... 是不是错了。
  • @blargman,是的,这是错误的。您也许可以通过类型转换来强制工作,但由于 %d 用于打印有符号整数,它可能不是一个好的选择。
  • +1: ...但为了可移植性(并遵循标准中的 SHALL)不要忘记转换地址。 printf("%p\n",(void*)&amp;a);
  • @pmg,演员阵容有必要吗?我认为在 C 中与 void * 之间的转换是安全且自动的(6.3.2.2 第 1 段)。
  • @Carl:在可变参数函数中,编译器无法根据预期类型检查参数类型。转换为void* 不是自动的:可变参数函数中自动转换为intfloat 的一些低范围整数double
【解决方案2】:

当您打算打印任何变量或指针的内存地址时,使用%d 将无法完成这项工作,并且会导致一些编译错误,因为您尝试打印的是数字而不是地址,即使它确实有效,你也会有一个意图错误,因为内存地址不是数字。 0xbfc0d878 的值肯定不是数字,而是地址。

你应该使用%p。例如,

#include<stdio.h>

int main(void) {

    int a;
    a = 5;
    printf("The memory address of a is: %p\n", (void*) &a);
    return 0;
}

祝你好运!

【讨论】:

  • 0xbfc0d878 一个数字。 (void*)0xbfc0d878 不是。 %p 可能会使用一种人类可读的表示形式,看起来像一个数字(通常是十六进制),但这并不意味着指针就是数字。 (顺便说一句,这个问题在 2 年前就已经回答了。)
  • 为什么我们必须转换为(void*)
【解决方案3】:

要打印变量的地址,您需要使用%p 格式。 %d 用于有符号整数。例如:

#include<stdio.h>

void main(void)
{
  int a;

  printf("Address is %p:",&a);
}

【讨论】:

  • 虽然此代码可以回答问题,但提供有关 如何为什么 解决问题的附加上下文将提高​​答案的长期价值。
【解决方案4】:

看起来你使用 %p:Print Pointers

【讨论】:

    【解决方案5】:

    我试过在线编译器 https://www.onlinegdb.com/online_c++_compiler

    int main()
    {
        cout<<"Hello World";
        int x = 10;
        int *p = &x;
        printf("\nAddress of x is %p\n", &x); // 0x7ffc7df0ea54
        printf("Address of p is %p\n", p);    // 0x7ffc7df0ea54
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      • 1970-01-01
      • 2011-10-25
      • 2016-07-06
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      相关资源
      最近更新 更多