How a struct being typedef-ed to multiple names? How a struct being typedef-ed to multiple names? c c

How a struct being typedef-ed to multiple names?


It defines multiple typedefs, i.e multilple "names" for the same thing, while the second is a pointer to it.

The first one Get_noAllyp is the name given for the struct, while no_getOf represents a pointer to it.

I.e, writing no_getOf is completely the same as writing Get_noAllyp * in function signatures or variable declarations.


Here, there are two typedefs being crated in a short-hand manner. The above typedef can be broken down like

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 *


The 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 design of the C language is a bit clumsy here as it requires the struct type names to be always prefixed 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 the variable Get_noAllyp of type struct Alias and the variable no_getOf of type pointer to struct Alias.

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

Get_noAllyp is the same as struct Alias and no_getOf is the same as struct Alias * (i.e. a pointer to a struct Alias`).

Now you can write:

struct Alias x;struct Alias *y;

or

Get_noAllyp x;no_getOf y;

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