Record
MyStruct *s = malloc(sizeof(*s));
has the same effect as
MyStruct *s = malloc(sizeof(MyStruct));
except that now you only write MyStruct once. That is, the object that you select has a source type that is automatically detected, which reduces the likelihood of errors.
For example - it happened to me - you start with MyStruct . Then you decide that you need different MyStruct for different purposes. Thus, you get two different structures: MyStruct and AnotherStruct .
Then you reorganize your code and change some variables from MyStruct to AnotherStruct , and in the end
AnotherStruct *s = malloc(sizeof(MyStruct));
which could work in several circumstances or for a long time, until you do a little more, and at that moment changes in any structure are not completely connected. At this point, your code goes into kaboom.
eg.
typedef struct { int a; int b; } StructA; typedef struct { int a; int b; int c; } StructB; int main() {
source share