Zhou Ligong teaches you how to learn C language programming: construct an double pointer with an array

The first chapter is the basis of programming. This article is the second point in section 1.8.3: the pointer to the string and the pointer, and the third point: the string and the two-dimensional array.

> > > > 1. String and pointer pointer

Except for the operands of sizeof or &, the array name of a pointer array is equivalent to a double pointer constant in expressions, and its right value is the address of the first element of the array variable. For example:

Int main(int argc, char *argv[])

It is completely equivalent to

Int main(int argc, char **argv)

Obviously, if you want to access an array of pointers, it's most convenient to use a pointer to a pointer. However, you may accidentally write incorrect code if you're not careful. See Listing 1.45 for details.

Program list 1.45 a wrong sample program

1 #include <stdio.h>

2 int main(void)

3 {

4 char **pKeyWord;

5 static char * keyWord[5] = {"eagle", "cat", "and", "dog", "ball"};

6

7 pKeyWord = keyWord;

8 while(**pKeyWord != NULL)

9 printf("%s", *(pKeyWord++));

10 return 0;

11 }

Since the null character at the end of a string, '\0', is exactly equal to the value of a null pointer or NULL, the above code compiles and runs correctly, but it is still incorrect. Most compilers perform type conversions during compilation, converting **pKeyWord from a char type to a void *, or converting NULL from a void * to a char type. However, generally, a warning message is given at compile time because the null character is an integer type, and the null pointer is a pointer type.

The reason for writing such code shows that the programmer does not understand the difference between '\0' and NULL. If the compiler completely disables the conversion between char and pointer, the above code might fail to compile. It can be seen that each warning message from the compiler should be taken seriously, and the cause of the warning should be analyzed instead of just relying on the fact that the program compiles and runs correctly.

Listing 1.46 is a solution to Listing 1.45. It first checks whether *keyWord is a null pointer. If it is, it exits the loop. Otherwise, it prints the string and increments pKeyWord by 1 to move to the next string.

Listing 1.46 deals with multiple strings using pointer array variables and double pointer variables

1 #include <stdio.h>

2 int main(void)

3 {

4 char **pKeyWord;

5 static char * keyWord[6] = {"eagle", "cat", "and", "dog", "ball", "NULL"};

6

7 pKeyWord = keyWord;

8 while(*pKeyWord != NULL)

9 printf("%s", *(pKeyWord++));

10 return 0;

11 }

Since arrays of pointer types are also one-dimensional arrays, the arithmetic operations of double pointers are very similar to those of regular pointers. When pKeyWord points to keyWord, keyWord[i], pKeyWord[i], *(keyWord+i), and *(pKeyWord+i) are all equivalent ways to access elements of the pointer array. keyWord[i] points to the first address of the i-th string, which is the address of the first character of the i-th string. If the target variable pointed to by the pointer keyWord[0] is accessed, the value of *keyWord[0] is the first character 'M' of the string "Monday". Of course, *keyWord[0] can also be written as *pKeyWord[0], **keyWord, or **pKeyWord.

Listing 1.47 is another solution to Listing 1.45. It first checks whether *pKeyWord points to an empty string (i.e., a string containing only '\0', which means the 0th element is '\0'). If it is an empty string, it exits the loop. Otherwise, it prints the string and increments pKeyWord by 1 to move to the next string.

Program list 1.47 Handling multiple strings with pointer array variables and double pointer variables (2)

1 #include <stdio.h>

2 int main(void)

3 {

4 char **pKeyWord;

5 static char * keyWord[6] = {"eagle", "cat", "and", "dog", "ball", "NULL"};

6

7 pKeyWord = keyWord;

8 while(**pKeyWord != ""[0]) // ""[0] is equivalent to '\0'

9 printf("%s", *(pKeyWord++));

10 return 0;

11 }

In practical applications, the usage of ""[0] in Listing 1.47(9) is rare. Replacing it with '\0' would have the same function and slightly higher execution efficiency. Since string constants are read-only character arrays, the string constant "" is a string constant containing only the end character '\0'. That is, the 0th element of the array variable is '\0'. Since "" is an array variable, you can use the subscript operator to evaluate "" to get the specified array element, resulting in the value '\0' of ""[0]. In general, '\0' is not directly used in most programs, and the use of ""[0] in Listing 1.47(8) has two meanings:

● Corresponds to Program List 1.47 (5) to make the meaning of the program clearer. When *pKeyWord points to the last character of the 0th string, the loop ends. Note: The 0th element of the string and any other element of a string of 0s are not the same.

● Better portability. If the definition of the end of the character is modified in future C languages, the program doesn't have to change. For example, to support Chinese, if a Chinese character is used as a character, the character type must be corrected since it is no longer 8 bits, so its ending character may also be modified.

If you want to store static tabular data, of course, you should use arrays. The search program must know how many elements are in the array. The way to handle this problem is to pass an array length parameter. Another method used here is to use the pointer array method, which is:

Const char * keyWord[6] = {"eagle", "cat", "and", "dog", "ball", NULL};

That is, a NULL pointer is added at the end of the table. This NULL pointer enables the function to detect the end of the table when searching without knowing the length of the table in advance. The corresponding search sample program is shown in Listing 1.48.

Program list 1.48 Search sample program

1 int lookup(char *word, char *keyword[])

2 {

3

4 for(int i = 0; keyWord[i] != NULL; i++)

5 if(strcmp(word, keyWord[i] == 0))

6 return i;

7 return -1;

8 }

In C, the string array parameters can be char *keyWord[] or char **keyWord. Although they are equivalent, the former form can express the usage of the parameters more clearly.

The search algorithm used here is called sequential search, which checks each data one by one. If the number of data is small, the sequential search is also very fast. The standard library provides functions that can handle certain types of sequential search problems. For example, strchr and strstr can search for characters or substrings in a given string. If such a function exists for a data type, it should be used directly.

Although the search looks very simple, its workload is proportional to the number of data being searched. If the data you are looking for does not exist, and the amount of data doubles, the search workload will also double. This is a linear relationship, and the run time is a linear function of the data size, so this search is also known as linear search.

> > > > 2. String and two-dimensional array

There are two styles describing C-style arrays of strings: two-dimensional arrays and arrays of pointers, such as:

Char keyWord[][6] = {"eagle", "cat", "and", "dog", "ball"};

Char * keyWord[5] = {"eagle", "cat", "and", "dog", "ball"};

Among them, the first statement creates a two-dimensional array, as shown in Figure 1.16(a). The second declaration creates an array of pointers, each of which is initialized to point to a different string constant, as shown in Figure 1.16(b).

Figure 1.16 Rectangular array and irregular array

If you use a two-dimensional array instead of a pointer array to modify the program listing 1.44, the two methods use the same initialization list, and the for loop code that displays the string is also the same. So, just modify the declaration of the formal parameters and local variables. Since the value of an array variable name is a pointer, the function can run regardless of whether it is a pointer or an array name passed to the function. Although their declarations are different, in some ways, they are very similar, both representing five strings. When a subscript is used, it represents a string, but the types of the two are not the same. When using two subscripts, each represents a character. For example, keyWord[1][2] represents the third letter 't' of the string pointed to by the second pointer in the keyWord array. At first glance, the efficiency of a two-dimensional array seems to be lower because each line is fixed to the length of the longest keyword, but it does not require any pointers. On the other hand, pointer arrays also take up memory, but the memory space occupied by each string constant is only its own length.

If they are about the same length, the two-dimensional array form is more compact. If the length of each string is very different, most of the strings are very short, only a few are very long, then the use of pointer array form will be more compact, depending on whether the space occupied by the pointer is less than the space wasted by storing a fixed-length line. In fact, apart from very large tables, the difference between them is very small, so it doesn't matter at all. A two-dimensional array is a better choice unless you want to change any of these strings.

Wire Feeding Roller

Wire Feeding Roller is part of the spraying system.

Wire Feeding Roller,Welding Wire Feed Roller,Wire Feeder Roller,Wire Feed Drive Roller

Shaoxing Tianlong Tin Materials Co.,Ltd. , https://www.tianlongspray.com

Posted on