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.*/
The C language contains the typedef keyword to allow users to provide alternative names for the primitive (e.g., int) and user-defined (e.g struct) data types. Remember, this keyword adds a new name for some existing data type but does not create a new type.
You can't "typedef a struct", that doesn't mean anything.
You can declare a typedef name for a pointer to a structure or union type before you define the structure or union type, as long as the definition has the same visibility as the declaration. Typedef names can be used to improve code readability.
PLEASE don't typedef structs in C, it needlessly pollutes the global namespace which is typically very polluted already in large C programs. Also, typedef'd structs without a tag name are a major cause of needless imposition of ordering relationships among header files.
Using ‘typedef’, we cannot create a new datatype but define a new name for already existing type. This statement tells the compiler to recognize ‘bhanu’ as another name for ‘int’. ‘bhanu’ is used to create another variable ‘a’ . ‘bhanu a ‘declares ‘a’ as a variable of type ‘int’.
Using ‘typedef’, we cannot create a new datatype but define a new name for already existing type. This statement tells the compiler to recognize ‘bhanu’ as another name for ‘int’. ‘bhanu’ is used to create another variable ‘a’ .
As we know that – to access the structure members, we need an object of the structure – that is known as a structure variable. Structure variable declaration of simple structure declaration Syntax:
This is very used for example when working with linked lists in C The new defined type can be used just as other basic types for almost everything. Try for example to create an array of type student and see how it works. Structs can be copied or assigned but you can not compare them!
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.
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 *
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With