这个问题可能有一个非常简单的答案,但我无法看到它。
#include <stdio.h>
int main(void) {
int x = 0;
printf("\n%d\n%d\n",x,&x);
}
好的,printf输出了0(x)和2293752(&x)。我正在尝试找出一种方法,可以查看任何地址(&)并查看已存储在那里的内容。这只是为了进行实验。有帮助吗?
这个问题可能有一个非常简单的答案,但我无法看到它。
#include <stdio.h>
int main(void) {
int x = 0;
printf("\n%d\n%d\n",x,&x);
}
好的,printf输出了0(x)和2293752(&x)。我正在尝试找出一种方法,可以查看任何地址(&)并查看已存储在那里的内容。这只是为了进行实验。有帮助吗?
void* memAddr = (void*)0xAABBCCDD; //put your memory address here
printf("int value at memory address %p is %i", memAddr, *((int*)memAddr));
#include <stdio.h>
int main(void) {
int x = 0;
printf("\n%d\n%d\n%d\n",x,&x,*(&x));
}
/* resulting output will be 0, some memory address, and 0 again */
%p
并转换为void *
。 - Johannes Schaub - litblong x=<address>
printf("%x", *(int *)x);
long
不能保证能够保存一个地址,而且使用指针更加明确。 - Bastien Léonard要查看地址,您可以使用%p
printf("%p",&x);
int x=10;
int *p; //this is a pointer to int
p=&x; //now p has the memory location of x
printf("%d\n",*p); //this will print 10
void*
,因此需要进行转换。顺便说一下,标准不能保证它会以十六进制形式打印输出。 - Bastien Léonard