Create a two-dimensional array

Source: Internet
Author: User

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];
}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.