C language learning 10: array

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

catalogue

1. Basic concept of array
1.1 array
1.2 subscript
1.3 memory continuity of array elements
1.4 array initialization
2. Precautions for array use

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[4];//An array containing 4 character elements

① char indicates that the type of the array is character type.
② arr is the name of the array.
③ [4] 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[4] = {'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[0] = 'a';
② char arr[1] = 'b';
③ char arr[2] = 'c';
④ char arr[3] = '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 arr[0]char arr[1]char arr[2]char arr[3]
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[4] = {1, 222, 23, 67};//While defining the array, assign integer data to each element

② Character array

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

③ Floating point array

float scores[10] = {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[10] = {1, 222, 23, 67};//While defining the array, assign integer data to some elements
// arr[0] = 1; arr[1] = 222; arr[2] = 23; arr[3] = 67;
// At this time, arr[4]~arr[9] 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[10] = {0};
char arr[10] = {0};
float scores[10] = {0.0};
/* This assignment method is equivalent to assignment num[0] = 0; arr[0] = '\0'; scores[0] = 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[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
// Use this method to assign arr[0]~arr[9] to 1
int a[10] = 1;
// Use this method to assign arr[0] = 1; arr[1]~arr[9] 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[5] = {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[0] = 'a'; arr1[1] = ‘b’; arr1[2] = ‘c’; arr1[3] = ‘\0’; . The definition method of arr2 [], defined arr1[0] = 'a'; arr1[1] = ‘b’; arr1[2] = ‘c’; Arr1 [3] = 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