How to struct typedef-ed for multiple names?

I recently started to study structures and pointers, but something I did not quite understand about struct design. I understand the declaration of struct ie typedef struct Alias and its contents, but I do not understand Get_noAllyp and *no_getOf at the end of the statement. What are they? I also could not find a good source.

 typedef struct Alias { char *s_a_name; char **s_aliases; short *s_dumr; int s_get_sum; }Get_noAllyp, *no_getOf; /*Here, I don't understand this one. Where did these two variables come from? And one of them is a pointer.*/ 
+6
source share
3 answers

It defines several typedef s, that is, verbose β€œnames” for the same thing, and the second is a pointer to it.

The first Get_noAllyp is the name for the structure, and no_getOf is a pointer to it.

Ie, the entry no_getOf exactly the same as the entry Get_noAllyp * in function signatures or variable declarations.

+9
source

There are two typedef broken down shortly. The above typedef can be broken down as

 typedef struct Alias { char *s_a_name; char **s_aliases; short *s_dumr; int s_get_sum; }Get_noAllyp; typedef struct Alias * no_getOf; 

So,

  • Get_noAllyp represents struct Alias
  • no_getOf represents struct Alias *
+7
source

Code:

 struct Alias { char *s_a_name; char **s_aliases; short *s_dumr; int s_get_sum; } 

defines a new data type that has the name Alias and is a struct . The original C language design is a little clumsy here, because to use them, structure type names always have a prefix with the struct keyword when they are used.

This means the code:

 struct Alias { char *s_a_name; char **s_aliases; short *s_dumr; int s_get_sum; } Get_noAllyp, *no_getOf; 

declares a Get_noAllyp variable of type struct Alias and a variable no_getOf type pointer to struct Alias .

By Get_noAllyp typedef keyword in front, the identifiers Get_noAllyp and no_getOf become types (and not variables).

Get_noAllyp same as struct Alias and no_getOf same as struct Alias * (that is, a pointer to struct Alias`).

Now you can write:

 struct Alias x; struct Alias *y; 

or

 Get_noAllyp x; no_getOf y; 

declare x as a variable of type struct Alias and y as a variable of type pointer to a struct Alias .

+3
source

Source: https://habr.com/ru/post/987652/


All Articles