8.4. Operations on structures

Most of the operators we have been using on other types, like mathematical operators ( +, %, etc.) and comparison operators (==, >, etc.), do not work on structures by default. It is possible to define the meaning of these operators for the new type, but we won’t do that in this book.

On the other hand, the assignment operator does work for structures. It can be used in two ways: to initialize the instance variables of a structure or to copy the instance variables from one structure to another. An initialization looks like this:

point blank = { 3.0, 4.0 };

The values in curly braces get assigned to the instance variables of the structure one by one, in order. So in this case, x gets the first value and y gets the second.

Note

Prior to C++11, this syntax can be used only in an initialization, not in an assignment statement. So the following is illegal in C++03, and is probablly a compile error when using default compiler settings.

point blank;
blank = { 3.0, 4.0 };       // C++11 or later only

A simple work around exists. If you add a C style typecast:

point blank;
blank = (point){ 3.0, 4.0 };

That works.

It is legal to assign one structure to another. For example:

point p1 = { 3.0, 4.0 };
point p2 = p1;
cout << p2.x << ", " <<  p2.y << endl;

The output of this program is 3, 4.

Construct a block of code that correctly initializes the instance variables of a structure.

You have attempted of activities on this page