Please help me identify the parts of this typdef
and what each part does and how it can be used:
typedef struct my_struct
{
int a;
int b;
int c;
} struct_int, *p_s;
struct_int struct_array[5];
What I think they are, pleae correct if wrong:
typedef struct
is creating a new type.my_struct
is the name of this type but not used in the rest of the code. struct_int
is one instance of the type that we can use in the code. *p_s
is the pointer specifically to the one instance we created. struct_array
is an array of the instance we created. (This part confuses me since we already created an instance...) Also, when creating the array of structs, why do we use struct_int
instead of my_struct
?
typedef struct
is creating a new type.
The typedef
is defining struct_int
and p_s
, which are synonymous to struct my_struct
and struct my_struct*
respectively. The idea is to save you from having to type things out the long way and to improve readability (which isn't always the case).
my_struct
is the name of this type but not used in the rest of the code.
Because you would have to use my_struct
as struct my_struct
, but you've typedef
'd struct_int
to refer to that type and it's simpler.
struct_int
is one instance of the type that we can use in the code.
No, read the above. If you removed the typedef
, then you would be creating two variables struct_int
and p_s
.
*p_s
is the pointer specifically to the one instance we created.
No, read the above.
struct_array
is an array of the instance we created. (This part confuses me since we already created an instance...)
struct_array
is an array of type struct_int
, therefore it's an array of type struct my_struct
. No instance is created in the definition of your struct
.
Also, when creating the array of structs, why do we use
struct_int
instead ofmy_struct
?
Same as one of the questions above; you can use struct my_struct
or struct_int
, struct_int
is shorter and it'd defeat the purpose of typedef
ing struct_int
.
struct my_struct
is a name of a type, my_struct
is a tag name and not a type.
typedef
does not create types in C, but creates alias names for existing types.
Not quite! Here's what different part mean:
struct my_struct
: Here you are creating a new data type which you will refer to as struct my_struct
whenever you want to create a new instance of the object. For example you should say: struct my_struct a;
and not my_struct a;
typedef struct my_struct {} struct_int;
: Here you merely create an alias for struct my_struct
and name it struct_int
. From now on, you can say struct_int a;
instead of struct my_struct a;
typedef struct my_struct {} *p_s;
Here, you create a new alias for a data type that points to struct my_struct
. You can use it like this: p_s newStruct = malloc(sizeof(struct_int));
my_struct
is the name of this type of structure. It can later be used to declare more structures of this type.
struct_int
is the name of the alias created by typedef.
*p_s
is a pointer to a structure of the my_struct
(aka struct_int
) type. It can point to a structure of this type.
struct_array
is the name of an array of my_struct
s. However, since it is declared with the alias struct_int
, we should refer to it later as one.
Using the alias is a personal preference, really one or the other of my_struct
or struct_int
could be removed.
Your assumptions are partly correct. I'd like to go into 3. and 4.
If you removed the typedef
and had this:
struct my_struct {
// ...
} struct_int_v, *p_s_v;
Then you would have declared two variables struct_int_v
and p_s_v
with the types you expected (one being struct my_struct
and the other struct my_struct*
).
However, when introducing the stroage specifier typedef
you are telling C not to create a variable but a type, while the declaration syntax is identical.
Thus, struct_int
will be the type that the variable struct_int_v
would have and p_s
will be the type that the struct_int_v
variable would have.
Note that storage specifiers like typedef
(or static
for that matter) apply to all variables (while other qualifiers like e.g. const
or volatile
apply to types in contrast) up to ;
.
You mostly answered your own question, so I'll address the two remaining points I see:
First, the name of the type that the struct
definition creates is struct my_struct
. You could declare variables like
struct my_struct a;
struct my_struct b;
but you don't want to have to write struct
all the time so you create an alias using typedef
. It might be a little clearer to define the structure and the aliases separately:
struct my_struct {
int a, b, c;
};
typedef struct my_struct struct_int;
typedef struct my_struct *p_s;
This first defines a type called struct my_struct
, then defines struct_int
as an alias for the type struct my_struct
, then defines p_s
as an alias for the type struct my_struct *
. The code you wrote has all three of these things combined into one, but it means the same thing.
Second, the type alias p_s
has nothing to do with the array struct_array
. The *p_s
doesn't define an instance of the structure, because it's part of a typedef
: it defines a type alias instead. But even if there were no typedef
, and p_s
were therefore an actual pointer variable, the array defined afterward is a separate and unrelated variable.
typedef
to create convenience aliases for structure types, it's common to omit the structure name entirely: typedef struct { /* ... */ } my_struct_type;
. No sense having two different names for the same thing - Wyzard 2012-04-05 23:56
struct { int a, b; } my_two_ints;
. Since there's no typedef
, this defines an actual variable - Wyzard 2012-04-05 23:57