Reputation: 1962
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
Upvotes: 1
Views: 2155
Reputation: 508
The code seems to compile without warning or error on 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.
Upvotes: -1
Reputation: 753715
Your second printf()
should take a "%p\n"
format, and strictly a cast:
printf("%p\n", (void *)&y);
The number of machines where the cast actually changes anything is rather limited.
Upvotes: 4