Home>Article>Backend Development> What is the order in which two-dimensional arrays are stored in memory in C language?

What is the order in which two-dimensional arrays are stored in memory in C language?

青灯夜游
青灯夜游 Original
2021-07-12 11:47:45 27965browse

The storage order of two-dimensional arrays in memory is row-by-row, that is, after one row is placed, the second row is placed; that is, the "a[0]" row is stored first, and then the "a[1" ]" line, then store the "a[2]" line, and so on until all elements are placed; the elements in each line are also stored in sequence.

What is the order in which two-dimensional arrays are stored in memory in C language?

The operating environment of this tutorial: windows7 system, c99 version, Dell G3 computer.

The general form of two-dimensional array definition is:

dataType arrayName[length1][length2];

where dataType is the data type, arrayName is the array name, length1 is the length of the first dimension subscript, length2 is the second dimension subscript length.

We can think of a two-dimensional array as an Excel table, with rows and columns. length1 represents the number of rows and length2 represents the number of columns. To locate an element in the two-dimensional array, you must specify both row and column.For example:

int a[3][4];

defines a two-dimensional array with 3 rows and 4 columns, with a total of 3×4=12 elements. The array name is a, that is:

a[0][0], a[0][1], a[0][2], a[0][3] a[1][0], a[1][1], a[1][2], a[1][3] a[2][0], a[2][1], a[2][2], a[2][3]

If you want to express The element in row 2 and column 1 should be written as a[2][1].

You can also think of a two-dimensional array as a coordinate system, with an x-axis and a y-axis. To determine a point in a plane, you must know both the x-axis and the y-axis.

The two-dimensional array is conceptually two-dimensional, but it is stored continuously in memory; in other words, the elements of the two-dimensional array are next to each other, with no gaps between them. So, how to store two-dimensional arrays in linear memory? There are two ways:

  • One is to arrange by row, that is, after one row is placed, the second row is placed;

  • The other is The first is to arrange in columns, that is, after placing one column, put it in the second column.

In C language, two-dimensional arrays are arranged in rows.That is, the a[0] row is stored first, then the a[1] row is stored, and finally the a[2] row is stored; the 4 elements in each row are also stored in sequence. Array a is of type int, each element occupies 4 bytes, and the entire array occupies a total of 4×(3×4)=48 bytes.

You can think of it this way, a two-dimensional array is composed of multiple one-dimensional arrays of the same length.

For example:

There are 5 people in a study group, and each person has test scores for 3 courses. Find the average score of each subject and the overall average score of the group.

##Zhang Tao 80 75 92 王正华 61 65 71 李丽丽 59 63 70 Zhaoquanquan 85 87 90 ##zhoumengzhen For this question, you can define a two-dimensional array a[5][3] to store the scores of 5 people in 3 courses. Define a one-dimensional array v[3] to store the average score of each subject, and then define a variable average to store the total average score. The final programming is as follows:
-- Math C English
76 77 85
#include  int main(){ int i, j; //二维数组下标 int sum = 0; //当前科目的总成绩 int average; //总平均分 int v[3]; //各科平均分 int a[5][3]; //用来保存每个同学各科成绩的二维数组 printf("Input score:\n"); for(i=0; i<3; i++){ for(j=0; j<5; j++){ scanf("%d", &a[j][i]); //输入每个同学的各科成绩 sum += a[j][i]; //计算当前科目的总成绩 } v[i]=sum/5; // 当前科目的平均分 sum=0; } average = (v[0] + v[1] + v[2]) / 3; printf("Math: %d\nC Languag: %d\nEnglish: %d\n", v[0], v[1], v[2]); printf("Total: %d\n", average); return 0; }

Running results:

Input score: 80 61 59 85 76 75 65 63 87 77 92 71 70 90 85↙ Math: 72 C Languag: 73 English: 81 Total: 75

The program uses a nested loop to read the scores of all students in all subjects. In the inner loop, the scores of each student in a certain course are read in sequence, and these scores are accumulated. After exiting the inner loop (entering the outer loop), the accumulated scores are divided by 5 and sent to v[i] , which is the average score for the course. The outer loop loops three times in total, and calculates the average grades of each of the three courses and stores them in the array v. After all loops are completed, add v[0], v[1], v[2] and divide by 3 to get the overall average score.

Related recommendations: "

C Language Video Tutorial

"

The above is the detailed content of What is the order in which two-dimensional arrays are stored in memory in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn