Sorry if this has been asked before, I wasn't really even sure what to search for to come up with this.
When I create a typedef struct
, I usually do something like this:
typedef struct myStruct {
int a;
int b;
struct myStruct *next;
} MyStruct;
So I declare it with MyStruct
at the end. Then when I create functions that pass that in as a parameter, I write
int doSomething(MyStruct *ptr){
}
Yet I am collaborating with a friend on a project and I have come across his coding style, which is to also declare *MyStructP
like this:
typedef struct myStruct {
int a;
int b;
struct myStruct *next;
} MyStructR, *MyStructP;
And then he uses MyStructP
in his functions, so his parameters look like:
int doSomething(MyStructP)
So he doesn't have to use the *
in the parameter list. This confused me because when I look at the parameter list, I always look for the *
to determine if the arg is a pointer or not. On top of that, I am creating a function that takes in a struct I created and a struct he created, so my arg has the * and his does not. Ultra confusing!!
Can someone give insight/comparison/advice on the differences between the two? Pros? Cons? Which way is better or worse, or more widely used? Any information at all. Thanks!
It is generally considered poor style to hide pointers behind typedefs, unless they are meant to be opaque handles (for example SDL_GLContext
is a void*
).
This being not the case here, I agree with you that it's more confusing than helping.
The Linux kernel coding style says to avoid these kinds of typedefs:
Chapter 5: Typedefs
Please don't use things like "vps_t".
It's a mistake to use typedef for structures and pointers. When you see a
vps_t a;
in the source, what does it mean?
In contrast, if it says
struct virtual_container *a;
you can actually tell what "a" is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With