Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printf pointer argument type warning?

Tags:

c

clang

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

like image 614
EhevuTov Avatar asked Apr 25 '13 21:04

EhevuTov


1 Answers

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);
like image 162
ouah Avatar answered Oct 02 '22 13:10

ouah