|
กก |
Data structures.A data structure is a set of diverse types of data with different lengths grouped under a unique declaration. Its form is the following:
where model_name is a name for the model of the structure type and the
optional parameter object_name is a valid identifier (or identifiers)
for structure objects. Within key brackets { } they are the types and their
names corresponding to the elements that compose the structure.
If the structure definition includes the parameter model_name, that parameter becomes a valid type name equivalent to the structure. For example:
We have first defined the structure model products with two fields:
name and price, of different types. We have then used
the name of the structure type (products) to declare three objects of
that type:
apple, orange and melon.
Once declared, products has became a new valid type name like the fundamental ones int, char or short. And since then we have been able to declare objects (variables) of that type. The optional field object_name that can go at the end of the structure declaration serves to directly declare objects of the structure type. For example, to declare the structure objects apple, orange and melon like we have done previously we could also have done it this way:
Moreover, in cases like this last one in which we took advantage of the declaration of
the structure model to declare objects of the same one, the parameter
model_name (in this case products) becomes optional.
Although if model_name is not included it will not be possible to
declare more objects of this same model more ahead.
Once we have declared our three objects from a structure (apple, orange and melon) we can operate with the fields that compose them. For that we have to use a point (.) inserted between the object name and the field name. For example, we could operate with anyone of these elements as if they were standard variables of their respective types: apple.nameeach one being of its corresponding data type: apple.name, orange.name and melon.name are of type char[30], and apple.price, orange.price and melon.price are of type float. We are going to leave apples, oranges and melons and go with an example about movies:
The example shows how can we use the elements of a structure and the structure itself as normal variables. For example, yours.year is a valid variable of type int, as well as mine.title is a valid array of 50 chars. Notice that either mine and yours are also treated as valid variables of type movie_t when being passed to the function printmovie(). Therefore, one of the most important advantages of structures is that we can refer either to their elements individually or to all the structure as a block. Structures are a very used resource to build data bases, specially if we consider the possibility to build arrays of them.
Pointers to structuresLike any other type, structures can be pointed by pointers. The rules are the same than for fundamental data types: The pointer must be declared as pointer to the structure:
Here amovie is an object of struct type movies_t
and pmovie is a pointer to point to objects of struct type
movies_t. So, the following, as with fundamental types,
will also be valid:
pmovie = &amovie;Ok, now we will go with another example, that will serve us to introduce a new operator:
The previous code incorporates a new important operator, the operator ->. This one is a reference operator that is used exclusively with pointers to structures and pointers to classes. This allows us not to have to use parenthesis on each reference. In the example we used: movies->titlethat could be translated to: (*movies).titleboth expressions movies->title and (*movies).title are valid and mean that we are evaluating the element title of the structure pointed by movies. You must distinguish it clearly from: *movies.titlethat is equivalent to *(movies.title)and that would serve to evaluate the value pointed by element title of structure movies, that in this case would not have much sense. The following panel summarizes possible combinations of pointers and structures:
Nesting structuresStructures can also be nested so that a valid element of a structure can also be another structure.
Thus, after the previous declaration we could use the following expressions:
charlie.name(where, by the way, the last two expressions are equivalent). The way how structures have been used in this section is the one that it is used with C language's structures, nevertheless, in C++ the structure concept has been extended until the same functionality of a class with the peculiarity that all of its elements are considered public. But you will have more details on this topic in section 4.1, Classes.
| |||||||||||||||||||||||||||