Reputation: 1243
I've recently started to study about structs and pointers but there is something I didn't fully understand about the design of a struct
. I understand the declaration of the struct
i.e typedef struct Alias
and its content but I don't understand Get_noAllyp
and *no_getOf
at the end of the statement. What are they? I couldn't really find a good source either.
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.*/
Upvotes: 17
Views: 18033
Reputation: 72425
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
.
Upvotes: 13
Reputation: 134396
Here, there are two typedef
s 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 *
Upvotes: 15
Reputation: 5560
It defines multiple typedef
s, 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.
Upvotes: 22