Is there a good way to get rid of the following warning? I know it's a type issue in that I'm passing a unsigned long pointer and not an unsigned long, but does printf somehow support pointers as arguments? The pedantic in me would like to get rid of this warning. If not, how do you deal with printing de-referenced pointer values with printf?
#include <stdio.h>
int main (void) {
unsigned long *test = 1;
printf("%lu\n", (unsigned long*)test);
return 0;
}
warning: format specifies type 'unsigned long' but the argument has type
unsigned long *test = 1;
is not valid C. If you want to have a pointer to an object of value 1, you can do:
unsigned long a = 1;
unsigned long *test = &a;
or using a C99 compound literal:
unsigned long *test = &(unsigned long){1UL};
Now also:
printf("%lu\n", (unsigned long*)test);
is incorrect. You actually want:
printf("%lu\n", *test);
to print the value of the unsigned long object *test.
To print the test pointer value (in an implementation-defined way), you need:
printf("%p\n", (void *) test);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With