Well the second one is nonsense in many way. You don’t want to do that.
The second if used only – then think how would you pass it to the function. There are innumerous problem that you would face – one of them shown below :-
void fun( myStructure z){
printf("%d\n", z.id);
}
myStructure zz;
zz.id = 666;
fun(zz); <-- error over here.
This gives you error.
The correct choice is to use typedef
to create a type alias of the above declared struct
and use it.
Also the fundamental question is why would you do that. This is not the practical way to go about it.
Edit:
On reply to OP’s edit – on my response OP responded that the problem can be overcome by specifying a macro which takes it as argument. This comes with the drawback that now debugging will be much more difficult. And for a large project this will be even harder to work with.
The thing is – with
struct {
int id;
} a;
You have declared a type. But then in the same code if you do this
struct {
int id;
} b;
They won’t be of same type.
And those can’t be passed as a function because the type information associated with the struct
declared can’t be used so that we can specifically distinguish which is of what type – it is anonymous struct with no name information.
Every single time you create a type information that is not accessible to be reused later as type information is absent in the next lines because of anonymity.
From standard (Proving that it indeed declaring a type)
A structure type describes a sequentially allocated nonempty set of
member objects (and, in certain circumstances, an incomplete array),
each of which has an optionally specified name and possibly distinct
type.
1
solved C using typedef or #define to define structure [closed]