User-Defined Structures

A structure or struct is a user-defined collection of data. It is superior to using arrays because each item can have a different type and a different name. In the following section, you will see the advantages of using user-defined structures.

Arrays Are Not Enough

Data often appears in groups, or aggregates. For example, in the case study in Chapter 3, “Arrays and Algorithms,” which performs a simple stock price analysis, there are five numbers for each trading day: the four prices and the trading volume. Similarly, measurements from a meteorological station would include temperatures, air pressure, wind speed and direction, and so on. In these cases, all the numbers could be floating-point numbers, so an array could be used. Then mm[k][2] would represent, say, the third number of the kth measurement; however, this becomes very confusing. There are other disadvantages; you cannot copy arrays with simple assignment, for instance.

Now consider a case in which a data aggregate consists of different kinds of information. To characterize a person, you need two names, an ID number, a phone number (or two), and an address; the data types are mixed (for example, strings, integers). The C++ struct was designed to represent this kind of data.

Defining New Types

A struct (short for “structure”) definition creates a new type, and it does not reserve space for a variable. The new type defines how memory is to be organized, by declaring member variables or fields. After the new type is defined, you can use it to declare variables like any other C++ type. For example, consider a point on a graph, which has two coordinates x and y. The following code defines a two-dimensional point type P as a struct. Until a variable p of type P is declared, no memory is reserved; the variable is laid out as shown in Figure 5.1, with the member x at zero offset and the member y at an offset of 4 bytes (which is the size of an int.) To access the members, the member selection (or dot) operator (.) is used.

Figure 5.1. Memory layout of a struct Point, showing the offsets of the two fields.



struct Point {
  int x;
  int y;
};
;> Point p;
;> p.x = 10; p.y = 20;
(int) 10
(int) 20
;> Point q = p;
;> q.x + q.y;
(int) 30

Notice that you can initialize a struct variable by using another variable of the same type (Point q = p.) Similarly, you can assign such variables to each other. If you represented points as arrays of two ints, then you wouldn't be able to copy them as easily as in the preceding example. You can use expressions such as q.x anywhere you would use a normal C++ variable reference.

The following is an example of a structure that describes a person:

struct Date {
  short year;
  unsigned char month,day;
};
struct Person {
   string first_name;
   string last_name;
   Date birthday;
   long id;
   string postal_address;
   string email_address;
   long phone_no;
};
;> Person p;
;> p.birthday.year = 1965;
						

Notice that structures can contain just about any type, including other structures. Person contains a member birthday, which is a Date. In cases like this, you use the dot operator as many times as necessary (as in, p.birthday.year = 1965.) You can initialize structures by using a set of values within braces. UnderC does not currently support this, but with standard C++ you can initialize a Person structure as follows:

Person fred = { "Fred","Jones",{ 1965,03,02} ,2343222,"",
                 "[email protected]",4552334};

As with the enum and struct statements, the last brace in a struct is followed by a semicolon.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.75.217