[turn] C language programming skills: teach you how to define and use a string array

Time:2022-5-10

Achieve the purpose

When we write programs in C language, we often encounter the use of string array. The characteristic of this array is that each element in the array is a string, but the length of each string is different. If you use C + + language for programming, the implementation is relatively simple. You only need to directly select the string class of the standard template library and define an array of this class in the code. Now the question is, how to define such a string array in pure C language?

For example, for the following string array:

str = {

“I love C.”,

“I love C++.”,

“I love JAVA.”,

“I love Python.”,

“I love LabVIEW.”

}

Two definition methods in C language are given below.

Method 1: define a two-dimensional array of char type

This method is realized by defining a two-dimensional array of char type. Each string in the array can be obtained through the row index of the two-dimensional array. The size of the column limits the maximum number of characters that each string can contain. Therefore, when this definition is adopted, the size of the column must not be less than the maximum length of all strings in the array. For example, for the above array, the definition code of C language is as follows:

 

 

[turn] C language programming skills: teach you how to define and use a string array

When fetching each string of the array, you can index the row directly.

Method 2: define a pointer array pointing to char type

This method is realized by defining a pointer array pointing to char type. Each element in the array is a pointer, and each string in the array can be obtained through the pointer. For example, for the above array, the definition code of C language is as follows:

 

 

[turn] C language programming skills: teach you how to define and use a string array

Comparison of two methods

The above two methods can achieve our purpose, but they are different in memory occupation. The first method defines a two-dimensional array with 5 rows and 20 columns, that is, the byte length of each string is 20, so it needs to occupy 100 bytes, while the second method defines a pointer array. The byte length of the string pointed to by each pointer is its actual length, so its total length must be less than 100 bytes. Generally speaking, compared with the second method, the first method causes a waste of storage space.

String array definition and complete test code used

The complete C language test code of this example is given below, as shown in the figure below:

 

 

[turn] C language programming skills: teach you how to define and use a string array

The running results of the program are shown in the figure below:

 

 

[turn] C language programming skills: teach you how to define and use a string array

summary

Generally, when defining the array of strings in C language, the second method is recommended. Especially for the program development of single chip microcomputer and embedded system, we can make full use of the limited hardware resources and avoid the waste of storage space.

Reprint link:C programming skills: teach you how to define and use a string array