The first chapter is the basis of programming. This article is an array of pointers to 1.8.3. > > > >  1.  String and pointer array If there are the following definitions: int data0 = 1, data1 = 2, data2 = 3; Int *ptr0 = &data0, *ptr1 = &data1, *ptr2 = &data2; In fact, the address is also data, then the array can also hold pointers, so you can derive a constructor type based on the basic data type, that is, the same type of pointer variables are grouped together to form an array of pointers. Store an address in each element of the pointer array variable and distinguish them with subscripts. Although arrays and pointer arrays store data, there are subtle differences. Arrays store characters or values ​​of the same type, while arrays of pointers store pointers of the same type. such as: Int data0, data1, data2; Int *ptr[3] = {&data0, &data1, &data2}; The declaration is interpreted as an array of pointers to the int pointer (the number of elements is 3), and the "int *[3]" type name is interpreted as an array of pointers to the int (number of elements 3) type. That is, the ptr pointer array is an array of array elements with three pointers. The essence is an array of type int *[3], ptr[0] points to &data0, ptr[1] points to &data1, and ptr[2] points to &data2. Since ptr is declared as an array of pointers, ptr[0] returns an address. When the pointer is dereferenced by *ptr[i] (i=0~2), the content of this address is obtained, that is, *ptr[0]==1, *ptr[1]==2, *ptr[2] ==3. Of course, you can also use the equivalent pointer notation, and ptr+i represents the address of the ith element of the array. If you want to modify the contents of this address, you can use *(ptr+i). If you dereference **(ptr+i) twice, you can return the location of the allocated memory and assign it a value. For example, ptr[1] is located at address &ptr[1], expression ptr+1 returns &ptr[1], *(ptr+1) is used to get pointer &data1, and then **(ptr+i) is dereferenced to get &data1 Content "1". Thus, using the pointer notation of the pointer, let us know that the array of pointers is being processed. Obviously, as long as you initialize a pointer array variable to hold the first address of each string, you can reference multiple strings: Char * keyWord[5] = {"eagle", "cat", "and", "dog", "ball"}; Among them, the type of keyWord[0] is char*, and the type of &keyWord[0] is char **. Although these strings seem to be stored in the keyWord pointer array variable, the pointer array variable actually stores only the pointer, and each pointer points to the first character of its corresponding string. That is to say, all the characters of the i-th string are stored in a certain position in the memory, and the pointer pointing to it is stored in keyWord [i], that is, keyWord [0] points to ""eagle"", keyWord [1] points ""cat"", keyWord[2] points to "ant", keyWord[3] points to "dog", and keyWord[4] points to "ball". Although the size of keyWord is fixed, the string it can access can be any length. This flexibility is a powerful proof of C's powerful data construction capabilities. Since a pointer array is an array of elements as pointer variables, an array of character pointers can be used to process multiple strings. Obviously, it is better to use a switch statement to make a string into a pointer array. It can be seen that the random storage of data will be stored in two forms: storage address and stored value, as shown in Figure 1.14. An array contains pointers to actual information, rather than storing information directly in the storage space of array elements. In this way, you can flexibly store and sort data from any complex structure. Figure 1.14 Addressing method Conversely, the value-based storage packs the data sets of n elements in a fixed-size record block. The fixed size is s. The storage method is shown in Figure 1.15. Each string occupies a continuous size of 6 bytes. Storage block. Figure 1.15 Stored value To facilitate the description of multiple strings, a data exchange function will be designed. Since any data type pointer can assign a value to a void* pointer, you can take advantage of this feature and use the void* pointer as a formal parameter of the byte_swap() function to accept any type of data. Since the minimum length variable in C is a char type (including unsigned char, signed char, etc.), the result of sizeof(char) is 1, and the length of any other variable is an integer multiple of it. For example, in a 32-bit system, sizeof(int) is 4. Since the variable types of the C language are various, it is impossible to number each variable type, and swap does not care about the true type of the variable, so the variable type can be used instead of the variable type. The byte_swap function prototype is: Void byte_swap(void *pData1, void *pData2, size_t stSize); Among them, size_t is a predefined type in the C language standard library, specifically for saving the size of variables. stSize is the length of the variable, and pData1 and pData2 are the first and second parameters to be compared, respectively. When the return value is < 0, it means pData1 < pData2; when the return value = 0, it means pData1 = pData2; when the return value is 0, it means pData1 > pDta2. Here, any type of pointer can be passed into byte_swap(), which truly represents the meaning of the memory operation function. Regardless of the data type of the memory, the object it operates is just a piece of memory. Regardless of the type the user is passing in, from the C99 version, when assigning a void * type pointer to another type pointer, no cast is required. That is, one byte is exchanged at a time, so for int type data, it only needs to cycle 4 times. The premise is that the two variables must be of the same type. For example, the values ​​of the two variables a and b are exchanged. The usage is as follows: Byte_swap(&a, &b, sizeof(a)); The interface and implementation of the byte_swap() data exchange function are detailed in Listing 1.42 and Listing 1.43. Program Listing 1.42 swap data exchange function interface (swap.h) 1 #pragma once 2 void byte_swap(void *pData1, void *pData2, size_t stSize); Listing 1.43 Implementation of the swap data exchange function interface (swap.c) 1 void byte_swap(void *pData1, void *pData2, size_t stSize) 2 { 3 unsigned char *pcData1 = pData1; 4 unsigned char *pcData2 = pData2; 5 unsigned char ucTemp; 6 7 while (stSize--){ 8 ucTemp = *pcData1; *pcData1 = *pcData2; *pcData2 = ucTemp; 9 pcData1++; pcData2++; 10 } 11 } For a specific string, an application example of a pointer array is shown in Listing 1.44. Listing 1.44 Compare string size and output sample program 1 #include 2 #include 3 #include "swap.h" 4 5 const char * keyWord[5] = {"eagle", "cat", "and", "dog", "ball"}; 6 void show_str (void) //  Print keyWord data 7 { 8 for (int i = 0; i < sizeof(keyWord) / sizeof(keyWord[0]); i ++){ 9 printf("%s", keyWord[i]); 10 } 11 printf(""); 12 } 13 14 int main(int argc, char *argv[]) 15 { 16 show_str(); 17 18 if(strcmp(keyWord[0], keyWord[1]) < 0) 19 byte_swap(keyWord, keyWord +1, sizeof(keyWord[0])); 20 show_str(); 21 return 0; twenty two } Sewing Machine Motor,Dc Motor Sewing Machine,Industrial Sewing Machine,Brushless Servo Motor LISHUI SHUANGZHENG MOTOR CO.,LTD. , https://www.szservomotor.com