-1
#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 *?

  • 1
    What does the documentation say is the correc format specifier? – Yunnosch Sep 05 '17 at 05:56
  • 2
    There's no format specifier for type `struct Graph *`. The question makes no sense whatsoever until you specify what it is you want to print. The address stored in the pointer? Or something else? – AnT stands with Russia Sep 05 '17 at 05:59
  • http://de.cppreference.com/w/cpp/io/c/fprintf – Yunnosch Sep 05 '17 at 05:59
  • 2
    `printf("%u", graph);` --> `printf("%p", (void *)graph);` – BLUEPIXY Sep 05 '17 at 06:05
  • 2
    What output do you expect? – Jabberwocky Sep 05 '17 at 06:16
  • [Please see this discussion on why not to cast the return value of malloc() and family in C..](https://stackoverflow.com/q/605845/2173917) – Sourav Ghosh Sep 05 '17 at 06:24
  • What you *could* do to print `struct Graph`'s member `V` is: `printf("%d\n", *((int*) graph));` (using `%u` is wrong as it expects an `unsigned int`). Please note, that this only works as `V` is the **first** member of `struct Graph`. – alk Sep 05 '17 at 07:18

3 Answers3

5

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 type int. So you can do something like

 printf("%d", graph->V);

or, if you want to print the pointer returned by malloc() and stored into graph, you can do

  printf("%p", (void *)graph);

Finally, see this discussion on why not to cast the return value of malloc() and family in C.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

The compiler is right, graph has another type than unsigned int which would be printed by %u. You probably want graph->V since there is no other numerical member of the struct.

printf("%u", graph->V);

Also note your V has int type while you try to print an unsigned int.

UPDATE

What format specifier should I use for type struct Graph *?

For a pointer, you need the format specifier %p and a cast to the type that it accepts.

printf("%p", (void*)graph);

See online demo.

Melebius
  • 6,183
  • 4
  • 39
  • 52
  • 1
    `If you want to print the address of the pointer, you need to cast it to an (unsigned) int.` --> my downvote. Check the documentation. – Sourav Ghosh Sep 05 '17 at 06:02
  • Why would you cast a pointer to `unsigned int`? On many platforms pointers are much larger than `unsigned int`. What would be the point of such cast? What would be the meaning of its result? – AnT stands with Russia Sep 05 '17 at 06:03
  • @AnT Isn’t `int` the size of the native word of the platform which equals the size of a pointer? – Melebius Sep 05 '17 at 06:07
  • @Melebius nope, nopes. The cast is required. – Sourav Ghosh Sep 05 '17 at 06:15
  • @SouravGhosh What about the [implicit conversion](http://en.cppreference.com/w/c/language/conversion)? – Melebius Sep 05 '17 at 06:22
  • @Melebius for a varidic function? Pointer type? – Sourav Ghosh Sep 05 '17 at 06:25
  • 2
    @Melebius: Not necessarily (albeit indeed that was the intent). You want an integer type of the same width as a pointer - that would be `uintptr_t` or `intptr_t`. `unsigned int` on most real-life platforms is pinned at 32-bit width, while pointers can easily have 64. – AnT stands with Russia Sep 05 '17 at 06:25
  • 2
    `p` is defined for `void`-pointer only. So the code to print its value should be: `printf("%p", (void*) graph);` – alk Sep 05 '17 at 07:21
0

See what you will do my friend. Seems you want to print out the value you have assigned to the int v.

You are getting that errors because Variable v has a return type int of which you have not included it your printf... Hence

printf("%u", graph -> v);