Sometimes I see code like this (I hope I remember it correctly):
typedef struct st {
int a; char b;
} *stp;
While the usual pattern that I familiar with, is:
typedef struct st {
int a; char b;
} st;
So what’s the advantage in the first code example?
You probably mean this:
The asterisk is at the end of the statement. This simply means “define the type STP to be a pointer to a struct of this type”. The struct tag (
ST) is not needed, it’s only useful if you want to be able to refer to the struct type by itself, later on.You could also have both, like so:
This would make it possible to use
STto refer to the struct type itself, andSTPfor pointers toST.Personally I find it a very bad practice to include the asterisk in typedefs, since it tries to encode something (the fact that the type is a pointer) into the name of the type, when C already provides its own mechanism (the asterisk) to show this. It makes it very confusing and breaks the symmetry of the asterisk, which appears both in declaration and use of pointers.