• Nem Talált Eredményt

Structure type and structure variables

8. User-defined data types

8.1. The structure type

8.1.1. Structure type and structure variables

const unsigned wchar_t key = 0xCD;

int main()

wcout<<L"The encrypted text: "<< s << endl;

p=(wchar_t *)s.c_str();

while (*p) // decryption *p++ ^= key;

wcout<<L"The original text: "<<s<<endl;

}

8. User-defined data types

Arrays can store and process easily a set of data of the same type. However, most programming tasks require treating data of different types logically as one unit. In C++, there are many possibilities for that among which the most important ones are structures and classes since they provide the basis for the object-oriented solutions discussed in III. fejezet - Object-oriented programming in C++ of the present book.

In the following, we will learn more about structure, class, bit fields and union types from among the aggregate types. In this chapter, stress will be laid more on structures (structs). The reason for that is that all notions and solutions related to structures can be used for other user-defined types without exception.

8.1. The structure type

In C++, a structure (struct) is a type that is a set of many data of any type (except for void and function types).

These data members that are traditionally called structure elements or data members, have names that are only valid within their structure. (In other languages, the notion field is used for these elements; however, in C++, this name is attached to bit structures.)

8.1.1. Structure type and structure variables

A variable of a structure type can be created in two steps. First, a structure type has to be declared and then variables can be defined. The general declaration of structures is as follows:

struct structure_type {

type1 member1; // without an initial value!

type2 member2; . . .

typen membern; };

It should be noted that the curly brackets enclosing structure declarations have to be followed by a semicolon.

Data members should be declared according to the standard variable declaration rules of C++; however, initial values cannot be given for them. A structure variable (a structure) of the type above can be created by the already known method:

struct structure_type structure_variable; // C/C++

structure_type structure_variable; // only in C++

In C++, the name standing after the keywords struct, union and class can be used as type names without using the keywords. When typedef is used, the difference between the two programming languages disappears:

typedef struct {

type1 member1; type2 member2; . . .

typen membern; } structure_type;

structure_type structure_variable; // C/C++

Similarly to those of arrays, the definitions of structure variables may contain initial values. The lists of the expressions, separated from one another by commas, initializing the data members should be enclosed within curly brackets.

structure_type structure_variable= {initial_value_list}; // C/C++

It should be noted that most software development companies support structure definitions with typedef in order that confusions be avoided.

It should be noted that the visibility of member names within a struct is limited to the structure. This means that the same name can be used within the given visibility level (module, block), in other structures or as independent variable names:

struct s1 { int a;

double b;

};

struct s2 { char a[7];

s1 b;

};

long a;

double b[12];

Let's see an example for defining a structure type. The following data structure can be useful for a software cataloguing music CDs:

struct musicCD {

// the performer and the title of the CD string performer, title;

int year; // year of publication int price; // price of the CD

};

Variables can be defined with the type musicCD:

musicCD classic = {"Vivaldi","The Four Seasons", 2009, 2590};

musicCD *pmusic = 0, rock, tale = {};

From the structure type variables above, rock is not initialised whereas all members of the structure tale will have the default initial value corresponding to their type. The pointer pmusic does not refer to any structure.

We created a new user-defined type by having declared the structure. The data members of the variable of a structure type are stored in memory in the order of their declaration. On I.24. ábra - Structure in memory , we can see the graphical layout of the data structure created by the definition

musicCD relax;

It can be clearly seen from the figure that the names of the data members indicate their distance from the beginning of the structure. In general, the size of a structure is equal to the sum of the sizes of its data members.

However, in some cases, "holes" may appear between the members of a structure (when speed is optimized or when members are aligned to memory boundaries etc.). But it is always the punctual value that is obtained by the sizeof operator.

In most cases, the compiler is entrusted with the alignment of data members to memory boundaries. For a rapid access, it places the data by optimizing storage to the given hardware. However, if structures are exchanged by files between different platforms, the alignment used for saving has to be set in the reader program.

I.24. ábra - Structure in memory

In order to control the C++ compilers, we use the preprocessor directive #pragma, the features of which are completely implementation-dependent. For example, in Visual C++, the alignment to a byte boundary is done by the following directive, which has to be placed before the structure definition:

#pragma pack(1)

The argument of pack can be 1, 2, 4, 8 or 16. Without arguments, Visual C++ uses the default value for 32-bit systems: 4. Let's see the effects of the directive in the case of the following structure type.

#pragma pack(alignment) struct mix {

char ch1, ch2, ch3;

short sh1, sh2;

int n;

double d;

};

#pragma pack()