The world of C language (8)

foreword

Hello, C language (array)

1. What is an array?

Array: An ordered collection of data of the same type .

2. Array definition

data type array name [const expression]

The type name is used to specify the data type of each element in the array.

The array name should not be the same as other variable names, the array name is the first address of the array.

A constant expression is used to specify the number of elements in an array

We access array contents by array name and subscript, and subscript starts from 0.

for example:

one-dimensional array

int a[5]={0};//数组初始化

Two-dimensional array

int a[3][2]={0};

1. When initializing, you can only assign initial values ​​to some elements, and then other elements are automatically filled with 0. 

If you assign a value to the entire array when defining, you can not specify the number of array elements, and the system will automatically give the length of the array according to the number of initial values.

2. Array input and output

Since an array is a bunch of collections of the same type of data, we need to input collections when we enter them.

Here we use loop input and output

//输入
for(int i=0;i<5;i++)
{
    scanf("%d",a[i]);
}
//输出
for(int i=0;i<5;i++)
{
    printf("%d",a[i]);
}

 3. Use multiple loops when inputting and outputting multidimensional arrays

Demo: 2D array

for(int i=0;i<5;i++)
    for(int j=0;j<5;j++)
        scanf("%d",a[i][j]);//注意,数组名即是地址,不用加取地址符(&)

Today we briefly understood the definition and use of arrays. When we are dealing with large amounts of data, arrays will be our good helpers.

Thank you for reading this. I am a college student majoring in computer science. It is inevitable that there are mistakes in the blog post. I hope you will point it out in time.

 

Guess you like

Origin blog.csdn.net/m0_60653728/article/details/122097636