struct type_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
type_name
is a name for the structure type, object_name
can be a set of valid identifiers for objects that have the type of this structure. Within braces {}
, there is a list with the data members, each one is specified with a type and a valid identifier as its name.
|
|
product
, and defines it having two members: weight
and price
, each of a different fundamental type. This declaration creates a new type (product
), which is then used to declare three objects (variables) of this type: apple
, banana
, and melon
. Note how once product
is declared, it is used just like any other type.struct
definition, and before the ending semicolon (;
), the optional field object_names
can be used to directly declare objects of the structure type. For example, the structure objects apple
, banana
, and melon
can be declared at the moment the data structure type is defined:
|
|
object_names
are specified, the type name (product
) becomes optional: struct
requires either a type_name
or at least one name in object_names
, but not necessarily both.product
), and what is an object of this type (apple
, banana
, and melon
). Many objects (such as apple
, banana
, and melon
) can be declared from a single structure type (product
).apple
, banana
, and melon
) its members can be accessed directly. The syntax for that is simply to insert a dot (.
) between the object name and the member name. For example, we could operate with any of these elements as if they were standard variables of their respective types:
|
|
apple.weight
, banana.weight
, and melon.weight
are of type int
, while apple.price
, banana.price
, and melon.price
are of type double
.
|
|
Enter title: Alien Enter year: 1979 My favorite movie is: 2001 A Space Odyssey (1968) And yours is: Alien (1979) |
yours.year
is a valid variable of type int
, and mine.title
is a valid variable of type string
.mine
and yours
are also variables with a type (of type movies_t
). For example, both have been passed to function printmovie
just as if they were simple variables. Therefore, one of the features of data structures is the ability to refer to both their members individually or to the entire structure as a whole. In both cases using the same identifier: the name of the structure.
|
|
Enter title: Blade Runner Enter year: 1982 Enter title: The Matrix Enter year: 1999 Enter title: Taxi Driver Enter year: 1976 You have entered these movies: Blade Runner (1982) The Matrix (1999) Taxi Driver (1976) |
|
|
amovie
is an object of structure type movies_t
, and pmovie
is a pointer to point to objects of structure type movies_t
. Therefore, the following code would also be valid:
|
|
pmovie
would be assigned the address of object amovie
.->
):
|
|
Enter title: Invasion of the body snatchers Enter year: 1978 You have entered: Invasion of the body snatchers (1978) |
->
) is a dereference operator that is used exclusively with pointers to objects that have members. This operator serves to access the member of an object directly from its address. For example, in the example above:
|
|
|
|
pmovie->title
and (*pmovie).title
are valid, and both access the member title
of the data structure pointed by a pointer called pmovie
. It is definitely something different than:
|
|
|
|
title
of the structure object pmovie
(which is not the case, since title
is not a pointer type). The following panel summarizes possible combinations of the operators for pointers and for structure members:Expression | What is evaluated | Equivalent |
---|---|---|
a.b | Member b of object a | |
a->b | Member b of object pointed to by a | (*a).b |
*a.b | Value pointed to by member b of object a | *(a.b) |
|
|
|
|
Previous: Dynamic memory | Index | Next: Other data types |