[英]How to check address of pointer variable in C
I was trying to print the address of the pointer variable not the address where it is pointing to, could anyone assist me in achieving that? 我试图打印指针变量的地址而不是它所指向的地址,有人可以协助我实现吗? Below is what I am trying but it is showing warning which i am not able to resolve.
以下是我正在尝试但它显示警告,我无法解决。 Thanks!
谢谢!
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* y;
printf("%p\n",y);
printf("%x\n",&y);
y = (int*)malloc(sizeof(int));
printf("%p\n",y);
printf("%x\n",&y);
return 0;
}
Compilation warning: 编制警告:
Warning: format ‘%x’ expects argument of type ‘unsigned int’,
but argument 2 has type ‘int **’
Output:
0xb773fff4
bfa3594c
0x8361008
bfa3594c
Your second printf()
should take a "%p\\n"
format, and strictly a cast: 你的第二个
printf()
应采用"%p\\n"
格式,严格来说是一个强制转换:
printf("%p\n", (void *)&y);
The number of machines where the cast actually changes anything is rather limited. 演员实际上改变任何东西的机器数量相当有限。
The code seems to compile without warning or error on Visual Studio 2012. 代码似乎在Visual Studio 2012上编译时没有警告或错误。
#include <stdio.h>
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
int* y = 0;
printf("%p\n",y);
printf("%x",&y);
y = (int*)malloc(sizeof(int));
printf("%p\n", y);
printf("%x", &y);
return 0;
}
The only recommendation is that you initialize y when you declare it. 唯一的建议是在声明时初始化y。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.