1. dynamically create a two-dimensional array
Method 1: void fun (INT irow, int icol) {cstring ** ppdata;
Ppdata = new cstring * [irow]; for (INT I = 0; I <irow; ++ I)
{
Ppdata [I] = new cstring [icol];
} Method 2: void fun (INT irow, int icol) {cstring * ptemp;
Cstring ** ppdata;
Ptemp = new cstring [irow * icol];
Ppdata = new cstring * [irow]; for (INT I = 0; I <irow; ++ I)
{
Ppdata [I] = & ptemp [I * icol];
} Ppdata is the two-dimensional array we have created. Incorrect practice: void fun (INT irow, int icol) {cstring ** ppdata; ppdata = new cstring [irow] [icol];} error prompt: Error c2540: non-constant expression as array bound 2. Using a two-dimensional array (1) in an application, we sometimes need to pass a two-dimensional array as a parameter to a function, how is the form of this function correct? Void fun (cstring ** STR, int irow, int icol); correct void fun (cstring STR [] [], int irow, int icol); error, no number of columns in the specified array! Void fun (cstring * STR, int irow, int icol); correct. You can process STR in the function and convert it into a two-dimensional array (2). Now we have defined fun, void fun (cstring ** STR, int irow, int icol); then we need to input a two-dimensional array outside, how should we pass in? Correct practice: cstring ** test; // allocate the test space and initialize the value of test ...... Fun (test, 2, 1); incorrect practice: cstring test [2] [1]; // initialize the value for test ...... Fun (test, 2, 1); will prompt error: cannot convert parameter 1 from 'wtl: cstring [2] [1] 'to 'wtl :: cstring ** '(3) if you need to pass the pointer of the requested two-dimensional array to a struct member, you must first apply for the space struct info {cstring ** data; int X; int y;} incorrect statement: info * infotest; // apply for ppdata space infotest-> DATA = ppdata; error! The correct statement for the crash is: info * infotest = (Info *) malloc (sizeof (Info); correct // apply for ppdata space infotest-> DATA = ppdata; (4) delete the two-dimensional pointer to the array created in Method 1: Int J; For (j = 0; j <irow; j ++)
{
Delete [] ppdata [J];
}
Delete [] ppdata; [Note: Method 1: void fun (INT irow, int icol) {cstring ** ppdata;
Ppdata = new cstring * [irow]; for (INT I = 0; I <irow; ++ I)
{
Ppdata [I] = new cstring [icol];
}] For arrays created in Method 2: Delete [] ppdata; Delete [] ptemp; [Note: Method 2: void fun (INT irow, int icol) {cstring * ptemp;
Cstring ** ppdata;
Ptemp = new cstring [irow * icol];
Ppdata = new cstring * [irow]; for (INT I = 0; I <irow; ++ I)
{
Ppdata [I] = & ptemp [I * icol];
}}