Search code examples
cvariablesstructtypedef

Preceding the name of a typedef with *


I have recently discovered that you can precede with * a variable when defining a typedef struct in C.

This is an example of what I am talking about ( *book being the case):

typedef struct item {
    int id;
    float price;
} *book, pencil;

I don't really understand how this works.

Are those 3 variables equivalent in terms of data type?

struct item *foo;
book bar;
pencil *foobar;

Solution

  • All have the same type = pointer to the struct item.

    book type IMO is dangerous as it hides the pointer in the typedef making code less readable (for humans) and error prone