Arrays are closely related to pointers. The meanings of array names have at least two meanings:
1. Address of the first element in the data,
2. sizeof is the array size, not the pointer size.
Note: detailed introduction to Expert C Programming
The reference usage experience summary also contains relevant content
In programming, you need to pass a string array to a function. The test code is as follows:
# Include <stdio. h> <br/> # define M 2 <br/> # define n 100 </P> <p> void test (const char ** pstr) <br/> {<br/> int I = 0; <br/> for (I = 0; I <2; I ++) <br/>{< br/> printf ("array [% d] = % s/n", I, * (pstr + I )); <br/>}< br/> return; <br/>}</P> <p> int main () <br/>{< br/> char char_array [m] [N] = {"a.txt", "B .txt "}; </P> <p> test (const char **) char_array); </P> <p> return 0; <br/>}
Two strings in the array are expected to be output.
Unfortunately, a segment error occurs during actual compilation and running. (Check carefully and find the following issues that need attention:
1. The address passed to test is the first address of the Two-dimensional character array.
2. In the test parameter, the pstr type is Char **, and * (pstr + I) is char *
3. The memory structure of char ** can be considered to be composed of multiple consecutive char * elements, while the two-dimensional character array consists of M * n characters.
Memory
It can be found that when char ** is used to pass the first address of a two-dimensional string array. The same address space, but the retrieved values are different because of different element types.
When * pstr is used, the int value consisting of the first four characters is actually taken, not the starting address of the expected char_array [0. So it is easy to understand when a segment error occurs.
If you want to pass through char **, define the string
Char * str_array [m];
Str_array corresponds to an array of the char * type. In this way, there will be no problem.
View "C expert programming" and find that the "multi-dimensional array" in C language is actually an array, which can be seen as a vector (that is, a one-dimensional array of an object, its element can be another array ).
In the Empirical Summary of the C pointer, the problem of array names has been summarized as follows:
If an array type array [N] is declared, the array name array has two meanings:
1. It represents the entire array. Its type is type [N] and its size is N * (sizeof (type ));
2. It is a constant pointer. the pointer type is type * and the pointer type is type, that is, the type of an array unit. The Pointer Points to an array in the memory zone.
Unit 0th, the pointer occupies a separate memory zone. Note that it is different from the memory zone occupied by the array unit 0th. The value of this pointer cannot be modified, that is, similar
The expression of array ++ is incorrect.
In different expressions, array names can assume different roles.
1. sizeof (array) array represents the array itself
2. Size of the array unit calculated by sizeof (* array)
3. sizeof (array + n) calculates the pointer size, 4 for 32-bit machines