Structures. Part 1. Composite data types. The template of structure. Structural variable. Structures in the CLR. Declaring and initializing a structured variable

Structures. Part 1. Composite data types. The template of structure. Structural variable. Structures in the CLR. Declaring and initializing a structured variable


Content
  1. What is the difference between basic and composite data types?
  2. What types of data in C++ belong to composite or aggregate data types?
  3. What is called a structure in C++? What is the field of the structure variable?
  4. How are structures classified in the CLR? Native-structures. Managed-structures
  5. What is the general form of the declaration of a native-structure (a structure template)?
  6. What is the general form of a managed-structure declaration with ref and value qualifiers?
  7. What is a structure template? Examples of templates
  8. What is a structural variable? Ways to declare structural variables. Access to structured variable fields
  9. Where can I specify a template of structure in the CLR?
  10. How is the structure variable initialized when it is declared? An example of initializing a native-structured variable

1. What is the difference between basic and composite data types?

All data types in C++ are divided into:

Variables of basic data types consist of one data element. Variables of composite data types contain two or more data items.

 

2. What types of data in C++ belong to composite or aggregate data types?

Composite data types are also called aggregate or conglomerate data types. Composite or aggregate data types include:

  • arrays;
  • structures;
  • unions;
  • enumerations;
  • classes.

 

3. What is called a structure in C++? What is the field of the structure variable?

A structure is a union of one or more variables in one memory area that has the same name.

A structure is a grouping of variables that are related to each other according to some criterion.

The variables in the structure are called the structure fields. Such variables can be of different types. The names of fields (variables) in one template must be unique. However, in different patterns, names can be repeated.

 

4. How are structures classified in the CLR? Native-structures. Managed-structures

The CLR supports the declaration of two types of structures:

  • native-structures;
  • managed-structures.

Native-structures– these are the classical structures of the C/C++ language.

Managed-structures – these are structures that are placed in memory, which is allocated and managed by the CLR.

Managed structures can be declared with one of two qualifiers:

  • qualifier ref – means that the structure is a reference type;
  • qualifier value – it means that the structure is a value type.

The difference between the ref and value qualifiers is the way the memory is allocated to the pointer to the structure. In more detail work with pointers to the managed-structures is described here.

 

5. What is the general form of the declaration of a native-structure (a structure template)?

To declare a native-structure (or a structure template), use the keyword struct. General form of the structure declaration:

struct structure_type_name
{
    type item_1;
    type item_2;
    ...
    type item_N;
} structural_variables;

where

  • structure_type_name – structure template name. The structure template declares a new data type, which can be used later in the C++ program;
  • type – the type of data that is currently available in the program;
  • item1, item2, …, itemN – the names of the elements (variables) that make up the structure. Structure elements are also called fields of the structure;
  • structural_variables – these are variables that are structures. If the structure template is declared, the structural_ variables may be missing (see the following paragraphs).

 

6. What is the general form of a managed-structure declaration with ref and value qualifiers?

The general form of the structure declaration (structure template) with qualifier ref:

ref struct structure_type_name
{
    type item_1;
    type item_2;
    ...
    type item_N;
} structural_variables;

The general form of the structure declaration (structure template) with qualifier value:

value struct structure_type_name
{
    type item_1;
    type item_2;
    ...
    type item_N;
} structural_variables;

In the above descriptions:

  • structure_type_name – the name of the structure template. The structure template forms a new type of data, which can be used later in the program;
  • type – the type of data currently available in the program;
  • item1, item2, …, itemN – the names of the elements (variables) that make up the structure. Structure elements are also called fields of the structure;
  • structural_variables – these are variables that are structures. If a structure template is declared, the structural variables may not exist.

More detailed work with managed-structures is described in the topic:

Structures. Part 3. Working with managed structures in the CLR. Qualifiers ref and value. Declaring structural variables. Arrays of structural variables

 

7. What is a structure template? Examples of templates

To declare a structured variable, you must first specify a template for the structure. The template of the structure is also called the format of the structure. When specifying a structure template, memory is not allocated. A template is just a description (information), variables of which types should be included in the structure.

Example 1. Definition of the structure template, that describes the information about the hours worked by the employee.

The template contains the following information:

  • employee code;
  • name and surname of the employee;
  • number of hours worked;
  • rate per 1 hour.
// Information about the employee
// Structure template
struct Worker
{
    int code;   // employee code
    char name[50]; // name of the employee
    int hours; // number of hours worked
    float cost; // rate per 1 hour
};

In this example, the template is called Worker. The template describes the following fields (variables):

  • code – employee code;
  • name – name of employee;
  • hours – number of hours worked;
  • cost – rate per 1 hour.

Example 2. Declaring a template named Laboratory that contains information about the laboratory in the school:

  • laboratory number;
  • name of the laboratory;
  • area of the laboratory;
  • number of seats.
// information about the laboratory
// structure template
struct Laboratory
{
    int   number;
    char name;
    float area;
    int   places;
};

For managed-structures, the qualifier ref or value is specified before the word struct.

 

8. What is a structural variable? Ways to declare structural variables. Access to structured variable fields

A structured variable is a variable of type, that is specified in the structure template. If such a variable is declared, then memory is allocated for it. A structural variable is also called an instance of the structure.

A structural variable can be declared in several ways:

  • based on a template that was previously announced;
  • when the template is declared;
  • with the typedef qualifier.

Below are all 3 ways of declaring a structural variable, which is based on the Worker template describing information about the employee. An example for a native-structure is considered.

// information about emplooyer
// the template of native-structure
struct Worker
{
    int code;   // employee code
    char name[50]; // name of employee
    int hours; // number of hours worked
    float cost; // rate per 1 hour
};

Then the structural variable can be declared in the following ways.

Method 1. Declaration of the variable based on the previously defined Worker template. In this case, the structure template is first defined, then the variable (the structure instance) is declared.

// Method 1. Declaring of the variable from the previously created template
Worker w; // for the variable w is allocated memory

// filling the fields of variable
w.code = 334;
strcpy(w.name, "Johnson B.");
w.hours = 23;
w.cost = 12.85f;

In the above example, a variable named w is declared based on the previously described Worker template. Then, for example, the fields of the structural variable are filled.

Method 2. Declare a variable while the template is being defined. In this method, the declaration of the template of the structure and the creation of an instance of the structure (variable) are combined.

// Method 2. Declaring the variable w1 during the template declaration
struct Worker
{
    int code;
    char name[50];
    int hours;
    float cost;
} w1;

Access to the fields of variable w1 from another program code

// Filling in with the values of the fields of the variable w1
w1.code = 123;
strcpy(w1.name, "Williams D.");
w1.hours = 33;
w1.cost = 10.58f;

Method 3. Declare a variable based on the type created with the typedef qualifier.

Suppose that in some parts of the program announced a new type WORKER, which is based on the structure of Wrk, containing information about the employee:

// the 'typedef' keyword
// declaring of a new type WORKER
typedef struct Wrk
{
    int code;
    char name[50];
    int hours;
    float cost;
} WORKER;

Then, to use a variable named w2 of type WORKER, you need to write the following code:

// declaring a variable based on the 'typedef' keyword type
WORKER w2;
w2.code = 100;
strcpy(w2.name, "Typedef");
w2.hours = 33;
w2.cost = 21.33f;

In this method, it is not necessary to specify the name of the Wrk structure template. That is, the next entry is also correct

// after the word 'struct', the template name is missing
typedef struct
{
    int code;
    char name[50];
    int hours;
    float cost;
} WORKER;

 

9. Where can I specify a template of structure in the CLR?

In the CLR environment, it is recommended to declare a structure template or a new type based on the structure (typedef tool) in a separate “*.h” file, for example “MyStruct.h”.

Then it is convenient to include this structure to other modules of the program, for example:

#include "MyStruct.h"

 

10. How is the structure variable initialized when it is declared? An example of initializing a native-structured variable

 Let the template of Worker structure is given

// the template of native-structure
struct Worker
{
    int code;
    char name[50];
    int hours;
    float cost;
};

Then the initialization of the structural variable can be this.

Method 1. Declaring and initializing a variable after the ‘Worker’ template was previously set

Worker w = {
             150,
             "Andrews",
             45,
             18.45f
           };

int d;
d = w.code; // d = 150
float f;
f = w.cost; // f = 18.45

Method 2. Combining a template declaration and declaring a structured variable and it initializing.

// the template of the structure
struct Worker
{
    int code;
    char name[50];
    int hours;
    float cost;
} w1 = {
         200,
         "Armstrong W.",
         64,
         22.34f
};

...

int d;
float f;
d = w1.code; // d = 200
f = w1.cost; // f = 22.34

Method 3. If a type is specified using the typedef qualifier, then the initialization of the structure variable will be the next

// declaration of a new type WORKER
typedef struct
{
    int code;
    char name[50];
    int hours;
    float cost;
} WORKER;

...

WORKER W = {
             333,
             "Baxter N.",
             34,
             15.30f
           };

int d;
float f;

d = W.code; // d = 333
f = W.cost; // f = 15.30f

 


Related topics