# C language learning 10: array

Posted by lewisstevens1 on Mon, 10 Jan 2022 07:39:42 +0100

# 1. Basic concept of array

##### 1.1 array

A set of data of the same type stored in memory in turn is called an array. Each data it contains is called array element, and the number of data it contains is called array length.

for example

```char arr;//An array containing 4 character elements
```

① char indicates that the type of the array is character type.
② arr is the name of the array.
③  indicates that the array contains 4 elements of the same type.

Note: the data type of each element in the array must be the same and consistent with the type defined by the array

-------------------—
##### 1.2 subscript

Each element in the array has a sequence number, which starts from 0 and is called a subscript.

for example

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

According to the fact that the array has only four elements, it can be seen that the array has four subscripts, namely 0, 1, 2 and 3. Therefore, the following elements corresponding to each different subscript can be obtained:
① char arr = 'a';
② char arr = 'b';
③ char arr = 'c';
④ char arr = 'd';

Note: when accessing array elements, the subscript cannot exceed the specified value range. If it is too large or too small, it will cross the boundary, resulting in array overflow and certain errors

-------------------—
##### 1.3 The memory of each element of the array is continuous

An array is a whole, and the memory between its elements is continuous.

char arrchar arrchar arrchar arr
abcd

This feature needs to be kept in mind that there will be places to use this feature in subsequent pointer operations.

-------------------—
##### 1.4 Array initialization

The values of array elements are surrounded by "{}", and the values are separated by ","

① Integer array

```int arr = {1, 222, 23, 67};//While defining the array, assign integer data to each element
```

② Character array

```char arr = {'a', 'b', 'c', 'd'};//While defining the array, assign character data to each element
```

③ Floating point array

```float scores = {0.0};//While defining the array, assign floating-point data to each element
```

④ Some elements are assigned, and the unassigned part is automatically initialized to 0

```int arr = {1, 222, 23, 67};//While defining the array, assign integer data to some elements
// arr = 1; arr = 222; arr = 23; arr = 67;
// At this time, arr~arr are automatically initialized to 0

/* For short, int and long, the unassigned part is automatically initialized to the integer 0
For char, the unassigned part is automatically initialized to the character '\ 0'
For float and double, the unassigned part is automatically initialized to decimal 0.0
*/
```

⑤ All elements are initialized to 0

```int num = {0};
char arr = {0};
float scores = {0.0};
/* This assignment method is equivalent to assignment num = 0; arr = '\0'; scores = 0.0;
The other 9 elements are automatically assigned 0 or 0 '\0' Or 0.0
```

⑥ According to "⑤", only 0 or '\ 0' or 0.0 can be assigned collectively. To assign data other than 0 or '\ 0' or 0.0, each element must be assigned one by one. If you only assign 1 to all integer arrays, you need to use the following method:

```int a = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
// Use this method to assign arr~arr to 1
int a = 1;
// Use this method to assign arr = 1; arr~arr are all 0
```

⑦ Assign values to all elements. You can define an array without giving the array length

```int num[] = {1, 2, 3, 4, 5};
```

Equivalent to:

```int num = {1, 2, 3, 4, 5};
```

# 2. Precautions for array use

input

```#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

int main()
{
char arr1[] = "abc";// "abc" -- 'a' 'b' 'c' '\0'
// '\ 0' is the end flag of the string
char arr2[] = {'a', 'b', 'c'};// 'a' 'b' 'c'
printf("%s\n", arr1);
printf("%s\n", arr2);
return 0;
}
```

Garbled output It is not difficult to find that the output result of Arr1 [] is normal, while there is garbled code in the output result of arr2 []. The reason for this phenomenon is that Arr1 [] with this definition method, arr1 = 'a'; arr1 = ‘b’; arr1 = ‘c’; arr1 = ‘\0’; . The definition method of arr2 [], defined arr1 = 'a'; arr1 = ‘b’; arr1 = ‘c’; Arr1  = random value. Led to the emergence of garbled code.

Solution: set char arr2[] = {'a', 'b', 'c'}; Change to char arr2[] = {'a', 'b', 'c', 0};

```#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

int main()
{
char arr1[] = "abc";// "abc" -- 'a' 'b' 'c' '\0'
// '\ 0' is the end flag of the string
char arr2[] = {'a', 'b', 'c', 0};// 'a' 'b' 'c' '\0'
printf("%s\n", arr1);
printf("%s\n", arr2);
return 0;
}
```

Successful output of expected results ----—END----—

Previous review

★ C language learning 5: detailed explanation of machine number, truth value, original code, inverse code and complement code

★ C language learning 6: data type - > basic type - > integer type (int, short int, long int, char, etc.)

★ C language learning 7: introduction to ASCII code table and usage

★ C language learning 8: global variables and local variables

★ C language learning 9: This function or variable may be unsafe (Visual Studio 2013 solution to scanf function error reporting)

Topics: C Back-end