C language learning (7) array

1. Array

1.1 Why do we need arrays

1. In order to solve the storage and use problems of a large amount of data of the same type
2. To simulate the real world

1.2 Classification of arrays

There are three types of arrays:

1、一维数组
2、二维数组
3、多维数组

1.3 One-dimensional array

1.3.1 Defining arrays

1、为n个变量连续分配存储空间。
2、所有的变量数据类型必须相同
3、所有变量所占的字节大小必须相等

1.3.1.2 Basic operation

1.3.1.2.1 Initialization
  • fully initialized
int a[5] = {
    
    1, 2, 3, 4, 5}; //在定义数组时就完全赋值
  • Incomplete initialization, uninitialized elements are automatically zero
int a[5] = {
    
    1, 2, 3};// 在定义数组时部分值初始化
  • Not initialized, the values ​​of all elements are uncertain, all are garbage values
int a[5];
  • array of zeros
int a[5] = {
    
    0}; // 所有的元素都为零
  • Only when the array is defined can the overall assignment be made, in other cases the overall assignment is wrong
int a[5] = {
    
    1, 2, 3, 4, 5};
  • Initializing a Read-Only Array
    Sometimes it is necessary to make an array read-only, in which case values ​​can only be retrieved from the array, and new values ​​cannot be written to the array. To create such an array, the array should be declared and initialized with the const keyword.
// 使用const关键字声明后,该数组就只能读,不能修改
const char ch[5]={
    
    1, 2, 3, 4, 5};
1.3.1.2.2 Find

The lookup of the array is carried out according to the subscript index, and the index starts from 0.

#include <stdio.h>

int main(){
    
    

// 定义一个数组
char ch[5]={
    
    11, 21, 22, 33, 44};

// 取出数组的第2个值,因为索引是从0开始,ch[1]表示取出数组的第二个值。
printf("%d", ch[1]); // 返回21
    return 0;
}

When you need to take out all the values ​​​​of the array, you need to use a loop to take out all the values

#include <stdio.h>

int main(){
    
    

//定义数组
char ch[5]={
    
    11, 21, 22, 33, 44};
// 定义自增变量
int i;
// for循环取值
for(i=0; i<5; i++){
    
    
    printf("%d\n", ch[i]);
}
    return 0;
}

To retrieve all the character arrays, you can use the strlen() function to calculate the length of the array.

#include <stdio.h>
// 使用strlen函数需要引用此头文件
#include <string.h>

int main(){
    
    

//定义字符数组
char arr[]={
    
    'a', 'b', 'c', 'd'};
// 自增变量
int i;
// 循环取出字符数组的值。strlen计算arr数组的长度,但索引从0开始,需要减1
for(i=0; i<strlen(arr)-1; i++){
    
    
    printf("%c\n", arr[i]);
}
    return 0;
}
1.3.1.2.3 Assignment

After declaring an array, you can use the array subscript (or index) to assign values ​​to elements. You can directly assign a value to the subscript of the element to be modified by assignment.

#include <stdio.h>
#include <string.h>

int main(){
    
    

char arr[]={
    
    'a', 'b', 'c', 'd'};

int i;
// 将arr数组的第2个元素修成'e'
arr[1] = 'e';
for(i=0; i<strlen(arr)-1; i++){
    
    

    printf("%c\n", arr[i]);
}
    return 0;
}

1.3.1.2.4 Array Copy

In the C language, one array cannot be directly assigned to another array. Need to traverse the loop to assign to another array.

int a[5] = {
    
    1, 2, 3,  4, 5};
int b[5];
//如果要把a数组中的值全部复制给b数组

Wrong way:

b = a 	//error

Correct spelling:

for (i = 0; i < 5; ++i)
	b[i] = a[i]
1.3.1.2.5 Array Sorting

Here is a simple counterfeit sort first, and will continue to explain in detail later

# include <stdio.h>

// 冒泡排序函数
void bubble_sort(int arr[], int len){
    
    

    int i, j;
    int temp=0;
    for(i=0;i<len;i++){
    
    
        for (j = 0; j<len-i-1; j++){
    
    
        if (arr[j] > arr[j+1]){
    
    
            temp=arr[j];
            arr[j]= arr[j+1];
            arr[j+1]=temp;
            }
        }
    }
}

// 主函数
int main(void){
    
    

    int i;
    int arr[]={
    
    5, 2, 7, 6, 9, 3, 4, 12, 10};
    int len = sizeof(arr)/sizeof(int);
    // 
    bubble_sort(arr, len);

    for (i = 0; i < len; i++)
    {
    
    
        printf("%d\n", arr[i]);
    }
	return 0;	
}

1.4 Two-dimensional array

A two-dimensional array represents rows and columns. The following example represents a two-dimensional array with 3 rows and 4 columns. When a two-dimensional array is stored in memory, it is stored in a continuous memory space.

int a[3][4]; //总共有12个元素,可以当作3行4列看待。a[i][j]表示第i+1行第J+1列的元素

1.4.1 Initialization

A two-dimensional array, like a one-dimensional array, also has full initialization and incomplete initialization. Uninitialized values ​​are zero when not fully initialized.

Full initialization method one:

int a[3][4] = {
    
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

Full initialization method two:

int a[3][4] = {
    
    
{
    
    1, 2, 3, 4},
{
    
    5, 6, 7, 8},
{
    
    9, 10, 11, 12}
};

Incomplete initialization:

// 不完全初始化,后面的8个元素全为0
int arr[3][4]={
    
    1, 2, 3, 4}
1、在初始化时,行可以省略,但列不能省略。
2、初始化时建议使用方式二的方式初始化。

1.4.2 Basic Operation

1.4.2.1 Outputting the contents of an array
# include <stdio.h>

int main(void)

{
    
    
	int a[3][4] = {
    
    
		{
    
    1, 2, 3, 4},
		{
    
    5, 6, 7, 8},
		{
    
    9, 10, 11, 12}
	};
	int i, j;
	for (i = 0; i < 3; ++i)
	{
    
    
		for (j = 0; j < 4; ++j)
			printf("%-5d	",a[i][j]);	//-5表示左对齐五个字符。
		printf("\n");
	}
	return 0;	
}

1.5 Multidimensional arrays

1. Is there a multi-dimensional array
? No, because the memory is linearly unique, and an n-dimensional array can be regarded as a one-dimensional array in which each element is an n-1-dimensional array.
for example:

int a[3][4]

It can be regarded as a one-dimensional array with each element containing 4 small elements.

int a[3][4][5]

The array contains a one-dimensional array of 3 elements, but each element is a two-dimensional array of 4 rows and 5 columns.

Guess you like

Origin blog.csdn.net/qq_46292926/article/details/127568851