gogoWebsite

Character arrays in C language

Updated to 5 hours ago

Preface

  1. The code of this blog is implemented in VC6.0++;
  2. When I saw that the title of the blog seemed to be talking about character arrays, but in fact, in C language,Character arrays include character arrays and string arrays;
  3. Array knowledge is not very difficult, but some details need to be paid attention to. The cumbersome conceptual issues are not overemphasized, but only some problems that are easy to make mistakes and are easily overlooked;

Character arrays include character arrays and string arrays

Character array
concept: Obviously an array consisting of some characters; for example;
Define an array of characters: One-dimensional array:char function name [line constant expression]For example: char ch[5];
Two-dimensional array:char function name [column constant expression]For example: char ch[2][12]
Notice:
Since characters and integers can be converted between 0~127, int ch[10] can also be said to be a character type. This is not recommended, so this knowledge point can be ignored.
initialization

	1. char ch[12] = {'I',' ','L','o','v','e',' ','Y','o','u'};
	2. char ch[] = {'I',' ','L','o','v','e',' ','Y','o','u'};
	3. char [2][12] = {{'I',' ','L','o','v','e',' ','Y','o','u'},{'I',' ','L','o','v','e',' ','C','h','i','h','a'}};
	4. char [][12] = {{'I',' ','L','o','v','e',' ','Y','o','u'},{'I',' ','L','o','v','e',' ','Y','o','u'}}

Notice:

  1. During the initialization process, the compiler of the array item that has not been initialized will assign the default value '\0'; (The array item that has not been initialized in the integer array is assigned to 0). For example: char ch[5] = {'a','b','c','d'}; in ch[4] = '\0';
  2. In a two-dimensional array, rows can be written without writing, and columns cannot be written without writing;
    3)

String array
concept: Another form of character array; direct connection between characters;
Define an array of strings:One-dimensional arraychar variable name [row constant expression]; 2D arraychar variable name [row, constant expression] [column, constant expression]

initialization:

1. char str[11] = {"I Love you"};
2. char str[] = {"I Love you"};
3. char str[] = "I Love you";
4. char str[12] = "I Love you";
5. char str[2][14] = {{"I Love you"},{"I Love China"}};
6. char str[][14] = {{"I Love you"},{"I Love China"}};

Notice:
1)For character arrays,Constant expressionsGreater than or equal toNumber of characters in the array; and for string arrays,Constant expressionsOnly greater thanNumber of characters in string array, because: the compiler will assign a string to the end of each stringEnd sign–>'\0', so when initializing the string array, we must make a character space for the end flag in advance;

Compare the initialization of character arrays and string arrays

char a[] = {"I Love you"};
char b[] = {'I',' ','L','o','v','e',' ','Y','o','u'};
char c[] = {'I',' ','L','o','v','e',' ','Y','o','u','\0'};

From the array type:
a is an array of strings, and the system will assign the end glyph ‘\0’; the constant expression must be larger than the number of valid characters (at least 1);
b and c are both character arrays, and the system does not assign end characters; c[10] == '\0' is also added by us;

From the array items:
Although a and c are expressed in different forms, one character array and one string array; however, the two arrays can basically be equalized; the array length, output, etc. are the same, not only can they be assigned values ​​with %s and %c, but they can also be output with %s and %c;
b is a pure character type. You cannot use %s to assign values ​​to its input, nor can you use %s to output it, otherwise an error will occur;

To summarize: In fact, string arrays can be regarded as a special manifestation of character arrays; they solve the disadvantages of difficulty in inputting, initialization, and output of character arrays;

Introducing two functions

sizeof(object) function
Function: used to measure the memory space occupied by objects in brackets;

Example: (In VC6.0 environment)

int a;
 double b;
 char c;
 char d[4] = {'L','o','v','e'};
 char e[] = {"LOVE"};
 sizeof(a); //a is an integer, and an integer variable occupies 4 bytes of space, and the sizeof function returns value 4;
 sizeof(b); //b is double type, and a shaping variable occupies 8 bytes of space, and the return value of the function is also 8;
 sizeof(c); //c is a character type, one character takes up 1 character space, and the function return value is 1
 sizeof(d); //The character array d occupies a total of 4*1 character space, and the function returns value is 4;
 sizeof(e); //The string array e takes up 5*1 character space in total, and the function returns value is 5;

strlen(object) function
Note: This function does not measure other types of data like sizeof, and can only be used to measure string arrays;
Function: used to measure string arrayEffective length(The length here refers to the number of bytes);
example:

char a[]={"LOVE"};
 sizeof(a); //There are 5 characters in the string a, but the valid character is before ‘\0’, that is, LOVE; therefore the return value of the function is 4;

 /*The function of end identifier '\0' in string array*/
 char b[]={"LOVE\0LOVE"}; //The string pointed to by b is indeed: LOVE\0LOVE\0
 strlen(b); // When strlen() scans the string b, it is valid characters until the end identifier '\0' is encountered, and all the subsequent characters are invalid; therefore, strlen returns a value of 4;
 sizeof(b); //For the string b, the string pointed to is complete LOVE\0LOVE\0, and the space occupied is 10*1==10; so the return value is 10;

other

  1. The character array and string array are defined in the same way without initialization. If the value is assigned to it through scanf, the control of the input character type of the array, that is, the difference between %s or %c determines whether the array is a string array or a character array; here it should also be noted that the technical identifier of the string array occupies a space;