Reputation: 78
I am trying to get a better understanding of C and while trying to understand an error I had gotten myself, I came across this question. I eventually understood what I was doing wrong, but the code in that question is baffling me:
struct {
uint8_t time;
uint8_t type;
uint8_t phase;
uint8_t status;
} Raw_data_struct;
typedef struct Raw_data_struct Getst_struct;
void Getst_resp(Getst_struct Data);
As far as I understand, the issue was that the name of the structure was put in the wrong place, which meant the struct was defined anonymously, and thus the name "Raw_data_struct" wasn't available when the typedef was used.
However, what did the compiler do with the name? I checked the cpp reference site on this, but they mention only two types of struct declaration, one of which is the definition. The definition doesn't seem to make any allowance for anything after the struct-declaration-list. Yet, no compiler I tried identified that as an error (I tried both gcc and clang).
I would like to understand how the struct declaration is being used. Is the name "Raw_data_struct" being used for anything, or just being ignored? Is there a reason this is not an error?
Thank you for reading this through!
Upvotes: 5
Views: 1498
Reputation: 5265
A name placed after the struct definition is a variable in that scope of the type of that struct. In this case, Raw_data_struct
is a variable of the type of that anonymous struct.
Upvotes: 6