C语言 如何在C中打印内存地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30354097/
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
How to printf a memory address in C
提问by varlotbarnacle
My code is:
我的代码是:
#include <stdio.h>
#include <string.h>
void main()
{
char string[10];
int A = -73;
unsigned int B = 31337;
strcpy(string, "sample");
// printing with different formats
printf("[A] Dec: %d, Hex: %x, Unsigned: %u\n", A,A,A);
printf("[B] Dec: %d, Hex: %x, Unsigned: %u\n", B,B,B);
printf("[field width on B] 3: '%3u', 10: '%10u', '%08u'\n", B,B,B);
// Example of unary address operator (dereferencing) and a %x
// format string
printf("variable A is at address: %08x\n", &A);
I am using the terminal in linux mint to compile, and when I try to compile using gcc I get the following error message:
我在 linux mint 中使用终端进行编译,当我尝试使用 gcc 进行编译时,我收到以下错误消息:
basicStringFormatting.c: In function ‘main':
basicStringFormatting.c:18:2: warning: format ‘%x' expects argument
of type ‘unsigned int', but argument 2 has type ‘int *' [-Wformat=]
printf("variable A is at address: %08x\n", &A);
All I am trying to do is print the address in memory of the variable A.
我要做的就是打印变量 A 在内存中的地址。
回答by P.P
Use the format specifier %p:
使用格式说明符%p:
printf("variable A is at address: %p\n", (void*)&A);
The standard requires that the argument is of type void*for %pspecifier. Since, printfis a variadic function, there's no implicit conversion to void *from T *which would happen implicitly for any non-variadic functions in C. Hence, the cast is required. To quote the standard:
该标准要求的参数是类型void*的%p说明符。由于,printf是一个可变参数函数,对于 C 中的任何非可变参数函数,没有隐式转换到void *from T *。因此,需要转换。引用标准:
7.21.6 Formatted input/output functions (C11 draft)
7.21.6 格式化输入/输出函数(C11 草案)
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 的指针。指针的值以实现定义的方式转换为打印字符序列。
Whereas you are using %x, which expects unsigned intwhereas &Ais of type int *. You can read about format specifiers for printf from the manual. Format specifier mismatch in printf leads to undefined behaviour.
而您正在使用%x,它期望unsigned int而&A是类型int *。您可以从手册中了解printf 的格式说明符。printf 中的格式说明符不匹配导致未定义的行为。
回答by hmofrad
A workaround to use %xwith length specifier to print an intor unsigned intwithout compiler complaining about casting would be to use malloc:
使用%x长度说明符打印int或unsigned int不打印编译器抱怨强制转换的解决方法是使用 malloc:
unsigned int* D = malloc(sizeof(unsigned int)); // Allocate D
unsigned int D_address = *((unsigned int*) &D); // D address for %08x without warning
*D = 75; // D value
printf("variable D is at address: %p / 0x%08x with value: %u\n", D, D_address, *D);
Alternatively you can compile with gcc -wflag to suppress those casting warnings.
或者,您可以使用 gcc-w标志进行编译以抑制这些强制转换警告。

