How To Describe The Structure

Table of contents:

How To Describe The Structure
How To Describe The Structure

Video: How To Describe The Structure

Video: How To Describe The Structure
Video: How to... describe a process 2024, December
Anonim

A structure in most programming languages, in particular C ++, is a special data type, a collection of arbitrary elements. The content of the structure is determined at the time of its description, and its constituent elements can be of different types. The declaration and description of the structure is possible anywhere in the program, until it is called.

How to describe the structure
How to describe the structure

Instructions

Step 1

Usually the description of the structure contains its declaration as well. Since the structure is, in fact, a new data type, its name must be unique within the same program. In C ++, the struct keyword is used to declare a structure. Each element of the structure at the time of the description must also be declared, with a full indication of its type and the amount of memory it occupies. Example of a description: struct My_struct1 {int data1; char data2 [20]; float data3;}; Here My_struct1 is the name of the created structure. Items found in parentheses are called fields, they specify the content of the structure. Each instance of the new type will contain one variable int and a float, as well as an array of 20 character values (char).

Step 2

For further work, create an instance of the structure: My_struct1 Data_St; A pointer to a structure is created in the same way as for any other type using the "*" operator: My_struct1 * pointData_St;

Step 3

Often, when writing program code, situations arise when a new structure must be mentioned before it is fully described. In this case, use the short form of the declaration: struct My_struct2; However, it is still impossible to declare its objects after such a record, since it is impossible to set the amount of memory required for them. Therefore, before declaring instances, give the full form of describing the structure: struct My_struct2 {int data1, data2;};

Step 4

If the structure is supposed to be used only in one place in your program, combine the type declaration with the variable declarations. In this case, the structure name may not be specified. Declare the required number of instances immediately after the structure description, as indicated in the following example: struct {int data1; char data2;} exs1, exs2; Here exs1 and exs2 are objects of the created type and contain integer and character data.

Recommended: