C Language Advanced Tutorial - C Language Array (4): Multidimensional Array

insert image description here

1. The compilation environment of this article

The compilation environment of this article uses an integrated development environment: Visual Studio 2019
insert image description here

The link to the official website of Visual Studio 2019 is as follows

Visual Studio 2019 official website link
insert image description here

The features of the Visual Studio 2019 integrated development environment are

  • Visual Studio 2019 installs the Live Share code collaboration service by default.
  • New welcome window to help users write code quickly, improved search functionality, general performance improvements.
  • Visual Studio IntelliCode AI Help.
  • Better Python virtualization and Conda support.
  • And support for .NET Core 3.0 projects including WinForms and WPF, etc.

2. Declaration of two-dimensional array

A two-dimensional array can be declared as follows

float carrots[25][50];
  • This line of statement declares an array carrots, which contains 25 lines of 50 floating-point elements.
  • Note that each dimension is enclosed in its own square brackets.

Similarly, another two-dimensional array of floating-point numbers can be declared with the following statement

float numbers[3][5];

As with the vegetables in the field, it is convenient to have these arrays arranged in a rectangle. Arranging this array into 3 rows and 5 columns,
they are actually stored in memory in row order.

As shown below

  • How the array of 3 rows and 5 columns of elements is organized in memory
    insert image description here

  • It is easy to see that the rightmost index changes the fastest. Conceptually, an index on the left selects a row, and an index on the right selects an element in that row.

  • The diagram above illustrates how to think of a two-dimensional array as a one-dimensional array, where each element is itself a one-dimensional array.

  • The number array can be regarded as a one-dimensional array of 3 elements, each element in the array contains 5 elements of type float.

  • The 5 float elements of the first row are located at the memory address marked numbers[0], the 5 float elements of the second row are located at numbers[1], and the 5 elements of the last row are located at numbers[2].

  • Of course, the amount of memory allocated to each element depends on the type of variables the array contains.

  • Arrays of type double require more memory than arrays of type float or int.

The figure below illustrates numbers[4][10]how the array is stored, the array has 4 rows of 10 elements of type float

insert image description here

  • Because the type of the array element is float, it occupies 4 bytes on the machine, and the total memory occupied by this array is 4X10X4 bytes, which is 160 bytes.

3. Declaration of three-dimensional array

Three-dimensional arrays are an extension of two-dimensional arrays:

double beans[4] [10] [20]; // 4 fields, each with 10 rows of 20 beans
  • The array declared by this statement has 800 elements, which can be regarded as storing the output of bean plants. There are three fields of bean plants
    , and each field contains 10 rows and 20 columns of plants.
  • Arbitrary multidimensional arrays can be defined as desired.

4. Array declaration program compilation results

The compilation result of the array declaration program is as follows

已启动重新生成…
1>------ 已启动全部重新生成: 项目: 5.4-多维数组, 配置: Debug Win32 ------
1>Main.c
1>E:\Document\2-programmLanguageExper\C\C语言入门经典\C语言入门经典\5.4-多维数组\Main.c(10,8): warning C4101: “carrots”: 未引用的局部变量
1>E:\Document\2-programmLanguageExper\C\C语言入门经典\C语言入门经典\5.4-多维数组\Main.c(11,8): warning C4101: “numbers”: 未引用的局部变量
1>E:\Document\2-programmLanguageExper\C\C语言入门经典\C语言入门经典\5.4-多维数组\Main.c(14,9): warning C4101: “beans”: 未引用的局部变量
1>5.4-多维数组.vcxproj -> E:\Document\2-programmLanguageExper\C\C语言入门经典\C语言入门经典\Debug\5.4-多维数组.exe
1>已完成生成项目“5.4-多维数组.vcxproj”的操作。
========== 全部重新生成: 成功 1 个,失败 0 个,跳过 0==========

The result of running the compiled program is as follows

insert image description here

Five, the complete program

The complete program for this article is as follows

5.1 Main.h file program

#ifndef MAIN_H
#define MAIN_H


#include <stdio.h>
#include <stdlib.h>


#endif

5.2 Main.c file program

#define _CRT_SECURE_NO_WARNINGS

#include "Main.h"

int main()
{
    
    
	system("color 3E");

	// 二维数组的声明
	float carrots[25][50];
	float numbers[3][5];

	// 三维数组的声明
	double beans[4][10][20]; // 4 fields, each with 10 rows of 20 beans

	system("pause");
	return 0;
}

6. Summary

insert image description here

This article mainly introduces the way of declaring multidimensional arrays of arrays in C language advanced programming.
Introduced the two-dimensional array declaration method.
Introduced the two-dimensional array declaration method.

This article ends here.

  • Hope the multidimensional array of C language array in this article.
  • can help you.

Guess you like

Origin blog.csdn.net/m0_47419053/article/details/127342213