What is a C language two-dimensional array
What is the C language two-dimensional array, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
The row-column matrix in mathematics is usually described by a two-dimensional array, that is, the row is represented by the first dimension of the two-dimensional array, and the column is represented by the second dimension; all the problems in life that can be abstracted into objects and some attributes of the same type of objects are generally described by two-dimensional arrays.
For example, if you represent the performance data of a class of students in four courses, such as Chinese, math, foreign language, and C language. The problem can regard each student as an object, expressed by the first dimension of the two-dimensional array, if there are 50 students, the size of the first dimension of the two-dimensional array can be set to 50; the score can be regarded as the attribute of each object, and can be expressed by the second dimension of the two-dimensional array, each object (student) contains 4 attributes (4 courses), so the second dimension can be set to 4.
For example, if a company counts the sales data of a product in a certain month, the problem can regard a week as an object, a month contains 4 weeks, so there are 4 objects, the first dimension of the two-dimensional array can be set to 4; daily sales can be regarded as the attributes of each object, can be expressed by the second dimension of the two-dimensional array, the object (weekly) contains 7 attributes (7 days of daily sales), so the second dimension of the two-dimensional array can be set to 7.
The definition of two-dimensional array
Like the dimensional array, it supports not only the C89 standard two-dimensional static array, but also the C99 standard two-dimensional dynamic array or variable length array. Some C compilers have not been updated to support the syntax of the C99 standard, so variable-length arrays may report errors in some compilers. Unless otherwise specified, the two-dimensional array referred to in the tutorial defaults to a static array.
The general format of a static 2D array definition is:
Type array name [first dimension size] [second dimension size]
Among them, the sizes of the first and two dimensions are generally constant expressions.
For example:
Inta [4] [5]
An int type two-dimensional array a with 4 rows and 5 columns is defined.
Floatsc [3] [4]
A float type two-dimensional array sc with 3 rows and 4 columns is defined.
The following two-dimensional array is defined in the wrong form.
Inta [] [3]; / / error. The compiler cannot determine the required space
Inta [2] []; / / error. The compiler cannot determine the required space due to the lack of column subscript
Examples of dynamic arrays are as follows (for understanding only).
Intn=2
Inta [n] [3]; / / dynamic array, correct C99 syntax. But an error may be reported in some compilers
Inta [2] [n]; / / dynamic array, correct C99 syntax
An array that is not initialized at the time of definition, whose data elements are generally meaningless random values, such as:
Inta [2] [3]; / / the six elements of the array are random values.
A two-dimensional array can be regarded as a special one-dimensional array, and each element of it is an one-dimensional array. For example, define a two-dimensional array that represents the scores of three students in four courses:
Intsc [3] [4]
A two-dimensional array sc with three rows and four columns is defined, which can represent three objects (students). From this point of view, the two-dimensional array can be regarded as an one-dimensional array containing three objects (students), three objects (elements) are: sc [0], sc [1], sc [2], where sc is the one-dimensional group name.
Each object (element) sc [I] is an one-dimensional array containing four attributes (four grades). The four attributes are: sc [I] [0] (Chinese), sc [I] [1] (mathematics), sc [I] [2] (foreign language), sc [I] [3] (C language). Each row represents a student and each column represents a course, forming a row-column matrix form as shown below.
After reading the above, have you mastered what is the method of C language two-dimensional array? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!