I tried to print an unsigned int*
in C. I used %X
but the compiler said:
" format
%x
expects argument of typeunsigned int
, but argument 3 has typeunsigned int*
".
I also used "%u"
but I got the same error again.
Can anybody help me?
I tried to print an unsigned int*
in C. I used %X
but the compiler said:
" format
%x
expects argument of typeunsigned int
, but argument 3 has typeunsigned int*
".
I also used "%u"
but I got the same error again.
Can anybody help me?
If you want to print a pointer, you need to use %p
format specifier and cast the argument to void *
. Something like
printf ("%p", (void *)x);
where x
is of type unsigned int*
.
However, if you want to print the value stored at x
, you need to dereference that, like
printf ("%u", *x);
If you want to print the pointer itself you should use the format %p
:
// Create a pointer and make it point somewhere
unsigned int *pointer = malloc(sizeof *pointer);
// Print the pointer (note the cast)
printf("pointer is %p\n", (void *) pointer);
Or if you want to print the value that the pointer is pointing to, then you need to dereference the pointer:
// Create a pointer and make it point somewhere
unsigned int *pointer = malloc(sizeof *pointer);
// Set the value
*pointer = 0x12345678;
// And print the value
printf("value at pointer is %08X\n", *pointer);
While the %p
format specifier is uncommon, most decent books, classes and tutorials should have information about dereferencing pointers to get the value.
I also recommend e.g. this printf
(and family) reference which lists all standard format specifiers and possible modifiers.