In the C language, the use of three-dimensional or three-dimensional arrays is not well supported, and the usage is also very low, and some simple analysis of three-dimensional arrays will be performed later, this article mainly uses two-dimensional arrays to explore some concepts and techniques related to array use in C language.
1. A possible declaration in the referenced form of VaR [I] [J]
When you see a two-dimensional array reference form like var [I] [J], do you know how it is declared? The answer is no. Some universality of pointers and arrays will make you unable to judge such a form of declaration prototype. For a two-dimensional array, the general declaration method is:
Int var [10] [12];/* standard two-dimensional array of int type */
It can be accessed through the VaR [I] [J] form. There are three possible declarations for reference in this form:
(1) int * var [10];/* array of int type pointers */
(2) int ** var;/* pointer to an int type pointer */
(3) int (* var) [12];/* pointer of the int array */
Obviously, this is very similar to the fact that the real parameter passed to a function is actually an array function and a pointer (the contest between arrays and pointers in the previous article has been described in detail ), for the same reason: the array name as the left value is treated as a pointer before compilation.
2. How to modify array parameters before Compilation
Passing an array as a parameter to the function will be modified by the compiler. In the function, the passed array name is actually used as a pointer, as in the first section, the VaR [I] [J] reference form may have four declaration methods. When these four declaration methods are passed as parameters to the function, how does the compiler modify it? Is a two-dimensional array like var [I] [J] modified to ** var?
In fact, the rule for modifying an array name to a pointer parameter is not a recursive definition. the array of an array is rewritten as an array pointer instead of a pointer, the following are the changes to the four declaration methods:
(1) array: Char var [I] [J]; when passed as a real parameter to the function, the matched format parameter is the array pointer char (*) [J];
(2) pointer array: char * var [J]; when passed as a real parameter to the function, the matched format parameter is the pointer char ** var;
(3) array pointer: Char (* var) [J]; when passed as a real parameter to the function, the matched format parameter is also the array pointer char (* var) [J];
(4) pointer: Char ** var; when passed as a real parameter to the function, the matched format parameter is the pointer char ** var;
In the above four cases, you may have understood this sentence: The only thing you have changed to is the array name, this is equivalent to modifying only the leftmost dimension as a pointer (not recursively precise). If the declaration is no longer in the form of an array name or has become a pure pointer (the profound meaning of an array name ), then the compiler will not make any changes to it.
Here is an example:
Generally, we can see the char ** argv (based on the input function of passing parameters) parameter in the main () function, which actually keeps the multi-string passed by the user, these strings are stored as character (string) pointer array char * temp [I] (temporarily called Temp) before being passed as real parameters. Temp is changed by the compiler to Char ** type, because temp is actually an array name.
3. How to Use array parameters
(1) One-dimensional array
One-dimensional array as a function parameter is the simplest form of passing parameters in an array. The form parameter is rewritten as a pointer. Therefore, an Convention is required to indicate the length of the array. There are two methods:
A adds an additional parameter to indicate the number of elements.
B assigns a special value to the last element of the array, prompting that it is the end of the array, but make sure that this special value does not appear in the normal element value.
(2) Two-dimensional array
Like the method of a one-dimensional array, the method B needs to add one more row to fill in all elements as special values. The use of two-dimensional array parameters will be described in detail later.
4. Use pointers to pass multi-dimensional arrays to Functions
The method described in the third section is clumsy and can mark the problem of array range, but this method cannot express the concept of "the boundary of this array can be changed in different calls, if each element of a two-dimensional array (a one-dimensional array) has different sizes, this cannot be expressed.
In C language, we need to know the length of each dimension to provide the correct unit length for the address operation. However, there is no way to pass a common multi-dimensional array to the function, even if the array name is rewritten as a pointer by the compiler (non-recursive rewriting, this can only solve one-dimensional arrays), in fact, we need to provide the length of all dimensions except the leftmost one. The following function declaration can be called as follows:
Void my_func (int A [] [5] [10]);
Call method:
Int B [10] [5] [10];
Int C [5] [5] [10];
My_func (B );
My_func (C );
But it cannot be called like this:
Int d [10] [10] [10];
Int e [20] [10] [5];
My_func (d );
My_func (E );
Such a call cannot pass the compiler.
That is to say: you can pass a special array with a predetermined length like a function, but this method cannot meet the general situation. Next we will explain this problem step by step.
1) method 1
My_func (INT array [10] [20]);
This method is simple, but it also serves as a small declaration method, because it can only process 10 rows and 20 columns of int type arrays, if you want a more common method of multi-dimensional array parameters, the function can operate arrays of any length.
2) method 2
My_func (INT (* array) [20]);
This ensures that the compiler is used as a pointer to an int array with 20 elements, but it is not universal for passing parameters in a two-dimensional array, because another 20 feels bad.
3) method 3
This is the last analysis in the second section:
Main (INT argc, char ** argv );
Of course, it may also be:
Main (INT argc, char * argv []);
The first is a pointer, and the other is a pointer array. Here we can declare a common function that can pass two-dimensional arrays:
My_func (INT ** array); or
My_func (int * array []);
In this way, you can set the last pointer element to null to identify the end of the two-dimensional array. In fact, we can use some technologies to solve the general declaration of one-dimensional and two-dimensional arrays, but the three-dimensional and more-dimensional arrays cannot be implemented very well, which is also an internal limitation of the C language.
5. The function returns an array.
Strictly speaking, it is impossible to return an array directly from the function. This is a restriction of the C language. However, a function can return a pointer to any data structure, including an array pointer.
For the method to return the pointer to an array, we must know some things:
1) dynamically allocate arrays in Functions
We know that if a function is declared as follows:
INT (* my_func () [5];/* returns a pointer to an array protecting five int elements */
You can declare such a return type in the function, and then allocate the memory to it by dynamically allocating the memory. After processing, the system returns the result. A typical application scenario of dynamic memory allocation is that we don't know how much memory to define, but it may be very small or large, and its size may change dynamically during running.
Note: When Using Dynamic Allocation inside the function and using it outside the system, you may forget to release the memory, resulting in Memory leakage. Therefore, remember to release the memory after use.
2) do not declare a local array in the function, and then return it from the function as the return value.
When the function expires, the local variables of the function will be released (recycled by the system). If you do this, fortunately, you may also get the data you want in a short period of time (in fact, the partial variables of the function will change in the process stack when the function expires ), but the sky knows what will happen later.
6. Conclusion
After studying this article and the previous article "contest between C language arrays and Pointers", I believe that I have a deep understanding of the arrays and pointers of C language, the features may need to be understood and understood in actual programming. In fact, I personally think we should try to use as few obscure features as possible in the C language. What is better if it can be simply solved?
There are a lot of materials to explain arrays and pointers, such as "clear and plain C pointers", which are especially unique in explaining pointers and arrays, but here I will make some summary of my own, which can be explained to the past. In the future, I can also take it back frequently..."