Definition and calling method of a two-dimensional array as a parameter Function
Geophoenix
In the process of C language programming, it is inevitable that two-or more two-dimensional arrays will be used as function parameters. In the previous programming process, I used to apply dynamic arrays, directly define high-dimensional arrays. This problem has recently been encountered during programming: There are the following test programs:
Voidtest (double ** X, int row, int col );
Voidtest (double ** X)
{
For (INT I = 0; I <row; I ++)
For (int K = 0; k <Col; k ++)
X [I] [k] + = 100.0;
}
Intmain (INT argc, char * argv [])
{
/*
Double ** X;
X = new double * [3];
For (INT I = 0; I <3; I ++)
X [I] = new double [3];
*/
Double X [3] [3];
For (INT I = 0; I <3; I ++)
For (int K = 0; k <3; k ++)
X [I] [k] = I * K;
Test (x, 3, 3 );
For (INT I = 0; I <3; I ++)
For (int K = 0; k <3; k ++)
Printf ("X [% d] [% d] = % E \ n", I, K, X [I] [k]);
Getch ();
Return 0;
}
During compilation, the system prompts cannot convert 'double [*] [3] 'to double **'.
Forcibly convert the call Method to the type: Test (double **) X), compile the code, run the error, and prompt that the code is out of bounds.
It is said that:Because the array allocated on the stack and the array allocated on the stack may be different in the memory arrangement, the directly defined array is stored in the stack area of the program, and the data occupies a continuous interval; the dynamically applied array is stored on the system's far heap, except that the last one-dimensional element is continuously stored, other dimension elements may not be in a contiguous memory area..
// Stack:
Int Ia [2] [2] = {2, 3, 4, 5}; // four elements are sequential memory segments.
// Stack:
Int ** P = new int * [2]; // only the rows in each row are arranged consecutively and not necessarily consecutively.
For (INT I = 0; I <2; I ++)
{
P [I] = new int [2];
}
For (INT I = 0; I <2; I ++)
{
For (Int J = 0; j <2; j ++)
{
P [I] [J] = Ia [I] [J];
}
}
Therefore, int ** P is used to point the array on the stack to the first address, because int ** P is used as the address pointer at a time instead of the array pointer on the stack, therefore, an error occurs when you perform secondary resolution.
If you are looking for a general equation, you can only use:
Void F (int * P, int row, int col) // returns the rows and columns of the array, which is not suitable for arrays on the stack.
{
For (INT I = 0; I <row; I ++)
{
For (Int J = 0; j <Col; j ++)
{
Cout <p [I * row + J] <"";
}
Cout <Endl;
}
}
Int main (){
//.........
Int Ia [2] [2] = {2, 3, 4, 5 };
F (int *) IA, 2, 2 );
}
Using the above general method is still relatively troublesome, which makes programming more difficult. To avoid this problem, you can use dynamic arrays to replace all the originally defined arrays with dynamic arrays, similar to the part of the Code that has been commented out in the example at the beginning, of course, this will also be troublesome in the future. After the lifecycle of the dynamic array is completed, the memory space must be released, which is a bit cool, however, you can directly use arrays, Which is simpler than the general method above.
What should I do if I insist on using directly defined arrays? There are several methods:
Method 1:
Voidtest (double (* X) [3], int row, int col );
Call method: Test (x, row, col );
Call method test (x, row, col );
Method 2:
Voidtest (Double X [] [3], int row, int col );
Call method test (x, row, col );
For multi-dimensional arrays as parameters, dimensions except the first dimension must be specified. Otherwise, the compilation may fail.
From the above reference to the directly defined array, it is difficult to use directly defined arrays. Once the dimension of the directly defined array is changed, the function definition must be modified accordingly, otherwise, the program will go wrong, which also increases the trouble of further program development. To solve this problem once and for all, we recommend that you use the dynamic array method, although you need to manually release the memory, however, it is difficult to solve the problem.
Reference Source:Http://topic.csdn.net/t/20060714/09/4879614.html