[C Language Inspector Training Camp Fifth Day] Knowledge about array strings

foreword

Today's C language training camp did not arrange the explanation of high-dimensional arrays, because one-dimensional arrays are often tested in the exam. Based on one-dimensional arrays, the teacher analyzed related issues such as array out-of-bounds, array distribution in memory, buffers and character arrays.

1. The definition of an array

Let's look at a case first:

To store shoes, suppose you divide the bottom layer of your closet into 10 consecutive compartments. At this time, it will be very convenient to ask others to help you take the shoes. For example, you can directly tell him to take the shoes in the third grid on the bottom layer of the closet. Also assume that now we have 10 integers stored in memory, for the convenience of access, we can use the array provided by C language to access multiple elements through a symbol.

也就是在我们使用的时候,如果需要用到多个类型相同的变量,我们大可以选数组,来简化我们的操作。

1. One-dimensional array

①. How to define

The definition format of a one-dimensional array is

类型说明符 数组名 [常量表达式]

For example:定义一个整型数组,数组名为 a,它有 10 个元素

int a[10];

②. Declaration rules

注意:声明数组时要遵循以下规则:

  • (1) The naming rules of array names are the same as that of variable names, that is, follow the naming rules of identifiers.
  • (2) When defining an array, you need to specify the number of elements in the array, and the constant expression in square brackets is used to indicate the number of elements, that is, the length of the array.
  • (3) Constant expressions can contain constants and symbolic constants, but cannot contain variables. In other words, the C language does not allow dynamic definition of the size of the array, that is, the size of the array does not depend on the value of the variable during the running of the program.
  • If you don’t need high-dimensional arrays, you don’t need high-dimensional arrays (one more dimension is a little more complicated, unless you have a special understanding of arrays)

Here's an example of a bad declaration (supported by the latest C standard, but better left unwritten):

int n;
scanf(“%d”, &n); /* temporarily input the size of the array in the program*/
int a[n];

Other common errors in array declarations are as follows:

① float a[0]; /* array size is 0 meaningless /
② int b(2)(3); /
cannot use parentheses /
③ int k=3, a[k]; /
cannot use variables to describe the array size */

③. Memory distribution

Array elements are distributed continuously in memory, and follow the principle that small subscripts are located at low addresses. The space occupied by each element in the array is related to the array type.
insert image description here

④.Initialization method

(1) When defining an array, assign initial values ​​to the array elements. For example,

int a[10]={0,1,2,3,4,5,6,7,8,9};
cannot be written as
int a[10];a[10]={0,1,2,3, 4,5,6,7,8,9}

(2) You can only assign values ​​to some elements. For example,

int a[10]={0,1,2,3,4};
defines that the a array has 10 elements, but only 5 initial values ​​are provided in the curly braces, which means that only the first 5 elements are assigned initial values, and the latter The 5
elements have a value of 0.

(3) If you want to make the values ​​of all elements in an array 0, you can write as

int a[10]={0,0,0,0,0,0,0,0,0,0};
orint
a[10]={0};

(4) When assigning initial values ​​to all array elements, since the number of data has been determined, it is not necessary to specify the length of the array.

For example,
int a[]={1,2,3,4,5};

2. Two-dimensional array

In addition to using one-dimensional arrays, two-dimensional arrays can also be used, and the definition method of two-dimensional arrays is roughly the same as that of one-dimensional arrays.

类型说明符 数组名 [常量表达式][常量表达式]

For example:定义一个整型数组,数组名为 a,它有 100 个元素

int a[10][10];

3. High-dimensional arrays

The C language supports arrays of 3 dimensions and above, but we generally don’t use them, so we don’t need to delve into them, we just need to understand them. When using arrays, you must avoid the problem of array out of bounds, which I will introduce below.
First look at the use of high-dimensional arrays:

//
// Created by Zhu Shichong on 2023/1/9.
//
#include<stdio.h>
main()
{
    
    
	int a[1][1][1];
    int b[1][1][1][1];
    int c[2][2][2][2][2];
    a[0][0][0]=1;
    b[0][0][0][0]=2;
    c[1][1][1][1][1]=3;
    printf("%d %d %d",a[0][0][0],b[0][0][0][0],c[1][1][1][1][1]);
    return 0;
}

数组元素的个数=定义时各维度下标大小连乘

2. Issues related to accessing array elements

1. Access out of bounds

After the array definition is introduced earlier, it is continuously distributed in memory, and the maximum array subscript of an array of 100 elements is 99, so what problem will we have when we access the element with subscript 100? There are roughly two situations:

  • The next space in the array memory is occupied by other variables: it will directly cause a program exception.
  • The next space in the array memory is not occupied by other variables: the dirty value in the memory (that is, garbled characters. What is in the memory will be read) will eventually lead to program exceptions.

It can be seen that the array out of bounds has a great impact on our program.

Maybe the text is not as strong as the picture, here is a case: you can try it yourself

//
// Created by Zhu Shichong on 2023/1/9.
//
#include <stdio.h>
//数组越界
int main()
{
    
    
    int a[5]={
    
    1,2,3,4,5}; //定义数组时,数组长度必须固定
    int j=20;
    int i=10;
    a[5]=6; //越界访问
    a[6]=7; //越界访问会造成数据异常
    printf("i=%d\n",i); //i 发生改变
    return 0;
}

insert image description here
有这种情况的原因是:
The compiler does not check whether the program's reference to the array subscript is within the legal range of the array. This unchecked behavior has advantages and disadvantages. The advantage is that there is no need to waste time checking some known correct array subscripts. The disadvantage is that doing so will not detect invalid subscript references.
一个良好的经验法则是:
If the subscript values ​​are calculated from values ​​that are known to be correct, then there is no need to check; if the subscript values ​​​​are generated by user-entered data, then they must be checked before using them to ensure that they are in a valid within range.

2. Array transfer

Let's look at a case first:

//
// Created by Zhu Shichong on 2023/1/9.
//
#include <stdio.h>
#include<string.h>
//一维数组的传递,数组长度无法传递给子函数
//C 语言的函数调用方式是值传递(将地址的值传递过去)
void print(int b[],int len)
{
    
    
    int i;
    for(i=0;i<len;i++)
    {
    
    
        printf("%3d",b[i]);
    }
    b[4]=20; //在子函数中修改数组元素
    printf("\n");
}
//数组越界
//一维数组的传递
#define N 5
int main()
{
    
    
    int a[5]={
    
    1,2,3,4,5}; //定义数组时,数组长度必须固定
    print(a,5);
    printf("a[4]=%d\n",a[4]); //a[4]发生改变
    return 0;
}

If you do not pass len when printing the array, it will result in the inability to obtain the length of the array, so that you cannot access all the elements in the array. If you want to access all the elements in the array in the function, it is necessary to pass the length of the array to passed in as parameters.
原因如下:
This is because the length of a one-dimensional array cannot be passed, so we use len to pass the number of elements in the array. The actual array name stores the first address of the array. When calling the function, the first address of the array is given to the variable b (in fact, the variable b is a pointer type, and the specific principle will be explained in the pointer section). In b[] Any numbers filled in square brackets are meaningless. At this time, we modify the element b[4]=20 in the print function, and we can see that the starting address of the array b is the same as the starting address of the array a in the main function, that is, the two are located at the same location in the memory. When the function is executed At the end, element a[4] in array a is modified.
insert image description here
insert image description here

Three, Scanf and character array

1. Character array initialization

The definition method of a character array is similar to the one-dimensional array introduced earlier. For example,

char c[10];

Initialization of character arrays can be done in the following ways.
(1) Initialize each character by assigning a value individually. For example,

c[0]=‘I’;c[1]=’ ‘;c[2]=‘a’;c[3]=‘m’;c[4]=’';c[5]=‘h’;c[6]=‘a’;c[7]=‘p’;c[8]=‘p’;c[9]=‘y’;

(2) Initialize the entire array. For example,

char c[10]={‘I’,‘a’,‘m’,‘h’,‘a’,‘p’,‘p’,‘y’}

The above two initialization methods are generally not used in work, because character arrays are generally used to access strings. The usual initialization method is char c[10] = "hello". Because the C language stipulates that the end mark of the string is '\0', and the system will automatically add a '\0' to the string constant, in order to ensure that the processing method is consistent, generally '\0' will be artificially added to the character array, Therefore
, the length of the character string stored in the character array must be 1 byte less than that of the character array. For example, char c[10] stores up to 9 characters, and the remaining 1 character is used to store '\0'. 如果末尾没有存储\0将会可能导致程序出现以下错误(一直沿着字符数组打印,直到内存中存储\0为止).
insert image description here
insert image description here

2.scanf reads characters

The most obvious is scanf碰见空白字符会直接阻断that when there is a space in a sentence, there is no way to directly use scanf to read that sentence into a character array. You can experience it by entering how are you into the following small program.

//
// Created by Zhu Shichong on 2023/1/9.
//
#include <stdio.h>
//scanf 读取字符串时使用%s
int main()
{
    
    
    char c[10];
    char d[10];
    scanf("%s",c);
    printf("%s\n",c);
    scanf("%s%s",c,d);
    printf("c=%s,d=%s\n",c,d);
    return 0;
}

The best solution is to use the gets,puts function

The gets function is similar to the scanf function for reading standard input. We already know that the scanf function considers that the reading is over when it encounters a space when reading a string, so when there is a space in the input string, we need to use the gets function to read it.

The format of the gets function is as follows:
char *gets(char *str);

The gets function reads characters from STDIN (standard input) and loads them into str (string) until a newline character (\n) is encountered. As shown in the example below, after execution, we enter "how are you", a total of 11 characters, you can see that gets will read spaces, and you can see that we have not initialized the array, but there is '\0' at the end , this is because
when gets encounters \n, it will not store \n but translate it into a null character '\0'.

The puts function is similar to the printf function and is used to output standard output. The format of the puts function is as follows:
int puts(char *str);

The function puts writes str (string) to STDOU (standard output). puts will print the "how are you" string stored in the array c to the screen, and print a newline at the same time. Compared with the printf function, puts can only be used to output a string, and at the same time print an extra newline, which is equivalent to printf( "%s\n", c)

Four, character array related functions

The str series of string manipulation functions mainly include strlen, strcpy, strcmp, strcat, etc. The strlen function is used to count the length of a string, the strcpy function is used to copy a string into a character array, the strcmp function is used to compare the size of two strings, and the strcat function is used to concatenate two strings together.
The function prototype is as follows:

头文件 #include <string.h>
size_t strlen(char *str);
char *strcpy(char *to, const char *from);
int strcmp(const char *str1, const char *str2);
char *strcat(char *str1, const char *str2);

对于传参类型 char*,直接放入字符数组的数组名即可。

Note the following points:

  • The calculation principle of the strlen function is to determine the length of the string by judging the terminator.
  • The strcpy function is used to assign the characters in the string to the target character array one by one. In the example, we copy c to d, that is, each character in c is assigned to d in turn, and the terminator is also assigned to d. Notice,目标数组一定要大于字符串大小,即 sizeof(d)>strlen(c),否则会造成访问越界
  • The strcmp function is used to compare the size of two strings ( 首先比较首字母ASCII值,第一个相同比较第二个依次类推). Since the string in the character array c is equal to d, the return value here is 0. If the string in c is greater than d, then the return value is the subtraction of different characters in c and d, i.e. cd (positive value); if the string in c is smaller than d, then the return value is still cd (negative value). How to compare the size of two strings? The specific operation is to start from the beginning and compare the ASCII code values ​​​​of the characters at the same position. If it is found to be unequal, it will return directly, otherwise it will be compared later. For example, the return value of strcmp("hello", "how") is −1, that is, "hello" is less than "how", because the first character h is equal, and then compare the character at the second position, the ASCII code value of e less than o, then return eo. 不同的标准中有可能返回值不同, but it must be that the two strings are the same and return 0, and return a negative value if the front is small and the back is large, otherwise it will return a positive value.
  • The strcat function is used to append a string to the end of another string. In the example, "hello" is stored in character array c, and we concatenate "world" in d with c, and the final result is "helloworld". Note that 目标数组必须大于拼接后的字符串大小,即 sizeof(c)>strlen(“helloworld”).

Today's sharing ends here! ! !

Guess you like

Origin blog.csdn.net/apple_51931783/article/details/128732898