Consider this piece of code
int main(void)
{
typedef struct {
int i;
} s;
struct {
s s;
} t;
return 0;
}
It compiles fine. Now take a look at this one
int main(void)
{
typedef struct {
int i;
} s;
s s;
return 0;
}
This code will not compile -
‘s’ redeclared as different kind of symbol.
Question: Why is it correct to have "s s;" as a declaration inside a structure, but not correct to have this definition inside a function?
In upper example member s
is a local to struct. You cannot use it without using t.s
syntax, so there is no conflict with structure type s
.
In lower example structure type s
, and variable s
are in the same scope, so it is unclear which you are referring to.
As a struct
member, the identifier s
is unambiguous, because you'll always address it as somestruct.s
or someptr->s
.
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