Programming language C (VII). Data Structures (I)
1991/09/01 Alegria Loinaz, Iñaki | Maritxalar, Montse Iturria: Elhuyar aldizkaria
As analyzed in chapter 2, the basic data of C are characters, integers, and real numbers. In addition, the following data types can be used in the drafting of C programs: tables, demonstratives, structures, textures and enumerated. On the other hand, while in languages such as the high-level PASCAL new data types can be defined, C does not support it, although the expression typedef allows to define synonyms of data types. Therefore, in this chapter and in the next chapter we will explain the definition and use of the data types and synonyms mentioned.
Tables and demonstrators
The table is the most widely used data structure in any language. The words vector, array, array, string and string are used as a table synonym, although sometimes they are not table subtypes.
The table consists of a set of variables of the same type that are saved and known with the same name. The table may have one or several dimensions, called in the first case vectors. Two-dimensional arrays are called arrays and while an index is used to identify components within the vector, two indexes will be used in arrays.
When the table components are characters, the table is called string, string, or string, with different characteristics.
Definition and use of tables
The data type of components for table definition, table name and number of components must be specified in the range. Use only mentions the name of the table to refer to the entire table. To express a component, on the contrary, it is necessary to mention the name and index between the maps. The index indicates the position within the component table, but while in most languages the value range of the index is from one to the number of components (N), in C it is used from zero (N-1). Keep in mind that the index that is determined in the range can be any full expression; constant, variable or arithmetic combination between them.
In program 1 you can see an example in which after reading the 12-month sales in a table the annual sale and monthly percentages are calculated and printed. To read the table we will use a function that we will define in program 3.
![](images/A51030.jpg)
In the definition the initialization of the tables can be carried out, separating the values of the components by commas and defining at the beginning and end of them. For example, the definition of the table that indicates the number of days you have each month would be:
int hil-egun [12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 31, 31, 31, 30, 30, 31, 31, 31};
If there is nothing, tables with zeros are initialized.
Demonstratives
As has been said, when using the name of a table without an index reference is made to the full table, but this does not mean the value of all components, but the reference of the place where the table is located in memory. This concept is the same as that of the exhibitor and that is why it is said that when the name of the table is used we specify its index. For this same reason, the assignment or assignment between the two tables is also performed as a component. It cannot be done with a simple assignment.
However, although there is a great tendency to use tables, demonstrators can be used with all types of data. Demonstratives are variable, so they must be defined and placed in memory, occupying part of the memory. Pascal demonstratives, although they must be defined, do not include parts of memory, since they are dynamic, but as C are static, for the definition the special character is used *.
The pointer indicates the memory direction of a data and is always placed in four bytes. In the definition you must specify the type of data you display, * and the name of the index variable.
![](images/A51030-a.jpg)
For use * and agents are used. * While * has the same function as the Pascal operator (value of the variable displayed by the demonstrator), it is specified against non-exhibiting variables to refer to its reference (or address) and not to value (or content).
Program 2 defines a value exchange function between both variables. Both parameters are data and results simultaneously. Therefore, it cannot be redeemed by value but by reference (as in any other language).
Special care must be taken when exhibitors participate in arithmetic operations, since the unit to which it is added or removed is the length of the displayed variable rather than being one.
So in the next part of the program
![](images/A51030-b.jpg)
The variable p1 (length of a character) is added to p2 two and p3 four (length of long variables). On the other hand, sums and rests between exhibitors are supported when the data types shown are the same.
Function parameters tables
In the first program we have not defined the function 'reading table'. Since its first parameter is the table, there are two options for determining the parameter: as an array or as an index, as can be seen in programs 3 and 4 (both equivalents).
![](images/A51030-c.jpg)
![](images/A51030-d.jpg)
The following differences must be taken into account:
- Indexes can be used in the first solution, so the working variable (i) is complete. In the second, however, it will be done without indexes and the working variable (paux) will be demonstrative.
- As the standard scanf function requires the reference data, it is used in the first solution (the table is of type [i] long), but in the second not (the paux pointer and therefore the reference is).
- When the second solution becomes paux ++ it is added to paux four (length corresponding to long) and when evaluating the expression p + os-as the number of OS is added to the index four times.
- In the main program, the reading table (salmen, 12) can be replaced with the reading table (salmen [0], 12).
Character strings
A string is a table with the special character ‘\Ø’ formed by characters at the end. The differences with the other tables are:
- It can be started with a constant String type. E.g. char title [Ø2] = “ANNUAL SALES”;
- It is not necessary to know the length (in functions and macros), since to detect the end it is enough to look for the character ‘\Ø’.
- In the functions of Scanf and Printf there is a special format for strings and not for other tables.
- In the standard library there is a set of functions for the string handling (strlen, strcpy, strcmp, ...)
Programs 5 and 6 explain the function of calculating the number of times a character appears in a string.
![](images/A51030-e.jpg)
![](images/A51030-f.jpg)
Other
- In multidimensional tables, both definition and use require a number in the range for each dimension.
For example
,int matrix [1Ø] [10]; /* 1Ø ilad1Ø column*/
- Demonstrator tables can be defined. For example, by defining char * hil-izen [12]. The month name table contains 12 displays, each of which refers to a string.
- Referenced by a pointer may be another.
Example:
int r, *p, **q;
r = 1Ø
p = r; /* *p = 1Ø */
q = p /* **q = 1Ø */
![](https://elia.eus/static/elhuyar/img/elia_RGB.png)
Gai honi buruzko eduki gehiago
Elhuyarrek garatutako teknologia