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.
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.
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.
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.
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.
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
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.
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).
The following differences must be taken into account:
A string is a table with the special character ‘\Ø’ formed by characters at the end. The differences with the other tables are:
Programs 5 and 6 explain the function of calculating the number of times a character appears in a string.
For example
,int matrix [1Ø] [10]; /* 1Ø ilad1Ø column*/
Example:
int r, *p, **q;
r = 1Ø
p = r; /* *p = 1Ø */
q = p /* **q = 1Ø */