Data structures - C++ Tutorials
Data structures - C++ Tutorials
Search: Go
Not logged in
C++
Information
Tutorials
Reference
Articles
Forum
In this case, where 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.
It is important to clearly differentiate between what is the structure type name (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).
Once the three objects of a determined structure type are declared (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:
1 apple.weight
2 apple.price
3 banana.weight
4 banana.price
5 melon.weight
6 melon.price
Each one of these has the data type corresponding to the member they refer to: apple.weight, banana.weight, and
melon.weight are of type int, while apple.price, banana.price, and melon.price are of type double.
https://www.cplusplus.com/doc/tutorial/structures/ 1/4
6/3/2021 Data structures - C++ Tutorials
15 {
16 string mystr;
17
18 mine.title = "2001 A Space Odyssey";
19 mine.year = 1968;
20
21 cout << "Enter title: ";
22 getline (cin,yours.title);
23 cout << "Enter year: ";
24 getline (cin,mystr);
25 stringstream(mystr) >> yours.year;
26
27 cout << "My favorite movie is:\n ";
28 printmovie (mine);
29 cout << "And yours is:\n ";
30 printmovie (yours);
31 return 0;
32 }
33
34 void printmovie (movies_t movie)
35 {
36 cout << movie.title;
37 cout << " (" << movie.year << ")\n";
38 }
The example shows how the members of an object act just as regular variables. For example, the member yours.year
is a valid variable of type int, and mine.title is a valid variable of type string.
But the objects 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.
Because structures are types, they can also be used as the type of arrays to construct tables or databases of them:
Pointers to structures
Like any other type, structures can be pointed to by its own type of pointers:
1 struct movies_t {
2 string title;
3 int year;
4 };
5
6 movies_t amovie;
7 movies_t * pmovie;
Here 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 = &amovie;
The value of the pointer pmovie would be assigned the address of object amovie.
https://www.cplusplus.com/doc/tutorial/structures/ 2/4
6/3/2021 Data structures - C++ Tutorials
Now, let's see another example that mixes pointers and structures, and will serve to introduce a new operator: the
arrow operator (->):
The arrow operator (->) 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
(*pmovie).title
Both expressions, 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:
*pmovie.title
*(pmovie.title)
This would access the value pointed by a hypothetical pointer member called 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:
Nesting structures
Structures can also be nested in such a way that an element of a structure is itself another structure:
1 struct movies_t {
2 string title;
3 int year;
4 };
5
6 struct friends_t {
7 string name;
8 string email;
9 movies_t favorite_movie;
10 } charlie, maria;
11
12 friends_t * pfriends = &charlie;
After the previous declarations, all of the following expressions would be valid:
1 charlie.name
2 maria.favorite_movie.title
3 charlie.favorite_movie.year
4 pfriends->favorite_movie.year
https://www.cplusplus.com/doc/tutorial/structures/ 3/4
6/3/2021 Data structures - C++ Tutorials
(where, by the way, the last two expressions refer to the same member).
Previous: Next:
Dynamic memory Other data types
Index
https://www.cplusplus.com/doc/tutorial/structures/ 4/4