Programming language C (VIII). Data Structures (II)

On this occasion we will analyze three new data types: structures or records, textures and enumerated.

Structures, packaging and listed

SYNONYMS

In the previous chapter we began to analyze the structured data, in which tables and demonstrators were exposed. On this occasion we will analyze three new data types: structures or records, textures and enumerated.

Structures

While in the table all data are of the same type, in the structure or record there is a dataset that should not be of the same type. The data that make up the structure correspond to a drive or element, the most common type of data that is stored on the disk. In the library file, for example, there is a record (or structure) of each book, where there is code, title, writer, number of pages, etc. (data of different types).

Program 1. Definition and management of structures.

Since it is impossible to use an index to determine a data in the structure by comparing it to the tables, each data or field is assigned a name, so to identify a data the structure and names of the field must be specified.

The C language uses the keyword and the “struct” operator to handle structures.

As in other types of data, we will differentiate the definition and use of the structures, but in this case the definition will be done in two steps, since while in the first the abstract structure is defined with the corresponding fields (definition of the structure), in the second the variable(s) of this type will be defined.

In program 1 we define and use the library file record.

Therefore, as can be seen, the names and types of the fields of the definition of the structure appear between the different kings and}, struct and name before and behind. In use, on the contrary, once the variable of this type is initially defined, the representation variable of the fields, the operator and the field name are specified.

If there are two variables of the same type of structure, the assignment between them with some compilers is possible by a single assignment, while with others it must be completed zonally. However, contrary to what happened with the tables, the name of a structural variable indicates its value and not its reference. Therefore, when determining a structure as a parameter you will be specified previously and what will be exchanged will be its index.

Structures and demonstrators

Structures and demonstrators can be combined. On the one hand, the component of a structure can be a demonstrator and, on the other, as mentioned above, demonstratives are used in the structure, especially in function calls.

As an example, let us think that to write the previous structure, that of the library, we have a function and as a parameter the reference of the structure of the book. This function is defined in program 2 and instead of using the operator the Preview operator will be used, as (* demonstrator). field and field of demonstrators are equivalent to C. The first program would replace the printf command with the following call:

print_lib_bat ()

Caved structures

Program 2. Function that prints the structure of the book.

The use of structural demonstrators allows referencing in a structure another variable of the same structure. This is widely used to define and use lists, rows, stacks, etc.

Program 3 presents a structure and function used to define the binary string tree. The function writes the tree from left to right using the recursive algorithm.

Program 3. Example of a complex structure.

Packaging

The same agents analyzed in the structures of definition and use of packaging (. Despite the use of -), the objective of this type of data is radically different, since although the fields described in both can be of different type, in the compilations these fields are different interpretations of the type that corresponds to a single field, while in the structures they are components of a set. Therefore, when reading a data collection type only one of the fields defined in it can be used. Although this type is used little, its most widespread use is because less memory is spent to treat the read of a file as a different type of data. For example, program 4 prints a piece of memori of 80 bytes, but according to a code they are 80 characters or 20 integers.

Program 4. Example of collection.

As seen in the program, the wraps can be combined with displays and boards. In this way the index of a collection has been sent to the function and the fields of the collection are tables. Wraps can also be combined with structures, as there may be structures within one structure, and vice versa, within the wrapper.

Listed

This type of data is used when the possible value of a variable is one of the components of a short group. What is done in this type can be done with characters or integers (assigning a code to each value), but for the program to be readable a very appropriate type of data occurs. The list definition consists of the enum keyword, the type name and possible values, separated by commas and between keys.

In program 5 you can see its application with a function that applies a different rate according to the day of the week:

Programme 5. Function that uses listed.

As we said in the previous one, C has no capacity to define new data types, but something similar can be done with the possibility of defining synonyms through the typedef operator.

With this method the programs improve readability, but a new type of real data is not created, since the only conclusion is that in compilation times it is a mere translation of strings

as in the case of macros

This way you can define the type of boolean data as you can see in Program 6:

Program 6. Use typedef to simulate the boolean type.
Babesleak
Eusko Jaurlaritzako Industria, Merkataritza eta Turismo Saila