When creating a struct you can do
struct name {
...
} var1, var2;
to create variables of type struct name.
if you want to do typedef the syntax is
typedef <existing type> <alias>
to do a typedef for a struct type you do
typedef struct name(optional) {
...
} Typename;
where typename is the alias and the struct part is the existing type.
I am wondering if you can combine the above code sections. ex do a typedef on a struct like in the second example and in the same line declare var1 and var2 like we did for the first example. this doesnt seem possible since the Typename seems to take the place of var1 and var2
No, not possible. There is no syntax for that.
Tangentially related, what is possible is having untagged struct:
struct {
...
} var1, var2;
As a side note, the problem is circumvented by not adding typedefs for structs.
In a more general note, code is easier to understand, if each statement does one thing (like define a variable or define a type). So even if this was possible, doing wouldn't necessarily be a good idea.
By this same principle, if you should decide to follow it, you should write:
struct name {
...
};
struct name var1, var2;
or, with typedef:
typedef struct name {
...
} name;
name var1, var2;
Note: if you don't need a pointer to same struct type in the struct itself (needed for linked lists and trees, basically), you can use untagged struct: typedef struct { ... } name;
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