#include<stdio.h>
#include<stdlib.h>
struct Graph
{
int v;
};
int main()
{
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph -> v = 1;
printf("%u", graph);
return 0;
}
But I get a warning regarding the format in line:
printf("%u", graph);
The warning is:
/home/praveen/Dropbox/algo/c_codes/r_2e/main.c|14|warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘struct Graph *’ [-Wformat=]|
What format specifier should I use for type struct Graph *
?
The C standard only specifies format specifiers for pre-defined types. The extended MACROs are there to print the fixed-width integers but there exist no format specifier for whole user-defined / aggregate types.
You don't have a format specifier for an array, a structure etc. You have to take individual elements/ members and print them according to their type. You need to understand what is the data (type) that you want to print, and use the appropriate format specifier.
In your case, you can print the member
V
, which is of typeint
. So you can do something likeor, if you want to print the pointer returned by
malloc()
and stored intograph
, you can doFinally, see this discussion on why not to cast the return value of
malloc()
and family in C.