Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Language: Names after the struct-declaration-list

Tags:

c

syntax

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!

like image 285
Alex Avatar asked Jun 07 '18 19:06

Alex


People also ask

What is the name after the struct in C?

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.

What is the declaration of the struct?

The struct-declaration-list specifies the types and names of the structure members. A struct-declaration-list argument contains one or more variable or bit-field declarations. Each variable declared in struct-declaration-list is defined as a member of the structure type.

How do you name a struct?

You can also define a struct, and declare/define a struct variable at the same time: struct Name { ... } myNameStruct; As before, this defines a variable called myNameStruct which is an instance of type struct Name ... But it does it at the same time it defines the type struct Name .


1 Answers

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.

like image 118
Thomas Jager Avatar answered Sep 22 '22 23:09

Thomas Jager