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

时间:2023-02-08 22:54:32

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

blargman

4 个解决方案

#1


71  

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

您想使用%p来打印指针。从规格:

p The 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);

#2


5  

When you intend to print the memory address of any variable or a pointer, using %d won'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 0xbfc0d878 is 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!

祝你好运!

#3


0  

Looks like you use %p: Print Pointers

看起来你使用%p:打印指针

#4


-1  

To print the address of a variable, you need to use the %p format. %d is for signed integers. For example:

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

#include<stdio.h>

void main(void)
{
  int a;

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

#1


71  

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

您想使用%p来打印指针。从规格:

p The 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);

#2


5  

When you intend to print the memory address of any variable or a pointer, using %d won'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 0xbfc0d878 is 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!

祝你好运!

#3


0  

Looks like you use %p: Print Pointers

看起来你使用%p:打印指针

#4


-1  

To print the address of a variable, you need to use the %p format. %d is for signed integers. For example:

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

#include<stdio.h>

void main(void)
{
  int a;

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