Structures in C ++. Part 2





c++ structures, structure c ++, struct c++. nested structures

With C ++ structures we have previously learned in the previous article. Continue to introduction.

Definition structure is desirable to have outside main() functions. Then it will be able to work with other programmer-defined function, as shown in our example of the first part. This declaration called external.

Инициализация. The elements of the structure can be initialized directly when declaring object.

We declare the object volume1 in string 13 and initializing it. It appears to initialize the array elements – in braces and separated by commas. These data will be recorded in the relevant elements of the structure of order. In what order are defined in the structure elements – and then will record values. The operation was still possible not to use (according to the C ++ 11).

Consider the following example for initializing a more complex structure WonderfulWoman :

c++ structures, structure c ++, struct c++. nested structures

The object of this structure can be initialized so:

c++ structures, structure c ++, struct c++. nested structures

But if the structure contains more than two or three elements-it is advisable not to do. It's a bit confusing and complicates the readability and understanding of the program.

Announcement of objects. Create the structure object can be directly during its determination. To do this, you must give the name of an object between a semicolon and a closing brace:

In c++ there is an opportunity to define a structure without a descriptor (unnamed type):

In this case it is necessary declare objects in determining the structure. In the main function, you will not be able to create other objects of this structure, since no descriptor. This technique makes sense to apply, if the structure of objects will be very small – 1 or 2.

Assignment (=) for structures. For objects of the same structure can be used the assignment operator = . Operation assign elements of one object values ​​of the elements of the second object.

This so-called element-wise or termwise assignment. On the screen:

c++ structures, structure c ++, struct c++. nested structures

It worked – all elements of the structure of the object volume2 steel elements are object volume1.

Memory occupied structure. Consider the example: using the operator sizeof, find out how much memory each structure element individually. We calculate the total amount of memory elements. Then apply sizeof to the object structure and see, sizes that do not match.

On the screen:

c++ structures, structure c ++, struct c++. nested structures

sizeof showed us how much memory each structure element HandsomeMan. We have summarized these values ​​and received 25. That is, the idea of ​​the structure should take 25 bytes of RAM. But when we apply the operator sizeof to the structure – get the value of 28.

The size of the object structure is not always equal to the sum of its elements size. This happens because of the alignment elements of different lengths. Therefore, the structure can meet the anonymous memory locations. If you need to know how many actually occupies in the memory structure – received sizeof , as in the example.

This video is from the previous lesson. Who has not watched – take time




We learned how to define structure, how to declare and initialize their objects. If we need to create more than 2-3 building structure, then it is better to create an array of structures. This topic was discussed in the next article.

To support our site – click on the piggy bank and choose any convenient way.

Newsletter of programming:

Structures in C ++. Part 2
5 (100%) 1 vote

4 thoughts on “Structures in C ++. Part 2

  1. strangely, but with the name of the structure in trouble, or rather when the input
    volume = { “name”, etc ;

  2. given an array, which stores information about the train schedule on this-
    dnyashny day: train number, name (i.e.. from where to where, for example, new-
    Russian-Moscow), time of arrival at the station and time of departure (clock,
    minutes). For this time to define, some of the trains are now
    station.

Leave a Reply

Your email address will not be published. Required fields are marked *