C语言 如何在C中打印变量地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5286451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:02:29  来源:igfitidea点击:

How to print variable addresses in C?

cpointersmemory-address

提问by nambvarun

When i run this code.

当我运行此代码时。

#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);
}

I keep getting this error in my compiler.

我在编译器中不断收到此错误。

/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 *'

Could you help me?

你可以帮帮我吗?

Thanks

谢谢

blargman

吹牛者

回答by Carl Norum

You want to use %pto print a pointer. From the spec:

你想用来%p打印一个指针。从规范:

pThe argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.

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

And don't forget the cast, e.g.

并且不要忘记演员阵容,例如

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

回答by Ron Nuni

When you intend to print the memory address of any variable or a pointer, using %dwon't do the job and will cause some compilation errors, because you're trying to print out a number instead of an address, and even if it does work, you'd have an intent error, because a memory address is not a number. the value 0xbfc0d878is surely not a number, but an address.

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

What you should use is %p. e.g.,

你应该使用的是%p. 例如,

#include<stdio.h>

int main(void) {

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

Good luck!

祝你好运!

回答by Amarendra Deo

To print the address of a variable, you need to use the %pformat. %dis for signed integers. For example:

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

#include<stdio.h>

void main(void)
{
  int a;

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

回答by Praveer Kumar

I tried in online compiler https://www.onlinegdb.com/online_c++_compiler

我试过在线编译器 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;
}

回答by skaz

Looks like you use %p: Print Pointers

看起来你使用 %p: Print Pointers