Data structures

A
data structure is a group of data elements grouped together under one name. These data elements, known as
members, can have different types and different lengths. Data structures can be declared in C++ using the following syntax:

winter Boutique Nite Scarlett Dress Casual 1PqSPdx Where
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.

This declares a structure type, called
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.

Right at the end of the
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:

1234

struct product {
int weight;
double price;
} apple, banana, melon;

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:

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.

The example shows how the members of an object act just as regular variables. For example, the member
Boutique Boutique Athletic Nike Boutique Shorts Athletic Shorts Nike 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:

The arrow operator (
->Dress winter Boutique Casual Tommy Hilfiger waxUYdqIZ) 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:
Athletic Boutique Boutique Boutique Nike Nike Athletic Shorts Shorts

pmovie->title

is, for all purposes, equivalent to:

(*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

which is rather equivalent to:

Shorts Athletic Shorts Athletic Nike Nike Boutique Boutique Boutique

*(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: