Dynamic assignment and parameter transfer of "reprint" two-dimensional array

Source: Internet
Author: User

This essay is reproduced, the original paste address: http://www.cnblogs.com/bigshow/archive/2009/01/03/1367661.html.

1. C-Language dynamic assignment of two-dimensional arrays(1) Second dimension known Code-1 char (*a) [n];//pointer to array a = (char (*) [N]) malloc (sizeof (char *) * m);p rintf ("%d\n", sizeof (a));//  4, pointer printf ("%d\n", sizeof (a[0]));//n, one-dimensional array free (a); (2) The first dimension is known Code-2 char* a[m];//pointer array int i; for (i=0; i<m; i++) a[i] = (char *) malloc (sizeof (char) * n);  printf ("%d\n", sizeof (a)),//4*m, pointer array printf ("%d\n", sizeof (A[0])),//4, pointer for (i=0; i<m; i++) Free (a[i]); (3) First dimension known, memory allocated at a time (guaranteed memory continuity) Code-3 char* a[m];//pointer array int i; a[0] = (char *) malloc (sizeof (char) * M * n); for (i=1; i<m; i++) A[i] = A[i-1] + N;   printf ("%d\n", sizeof (a)),//4*m, pointer array printf ("%d\n", sizeof (A[0])),//4, pointer Free (a[0]); (4) Two dimensions are unknown Code-4 char **a;int i; a = (char * *) malloc (sizeof (char *) * m);//allocation pointer array for (I=0; i<m; i++) { A[i] = (c Har *) malloc (sizeof (char) * n);//assigns each pointer to the array } printf ("%d\n", sizeof (a)),//4, pointer printf ("%d\n", sizeof (a[0]));// 4, pointer for (i=0; i<m; i++) {free (a[i]);}  Free (a); (5) Two-dimensional unknown, one-time allocation of memory (guaranteed memory continuity) Code-5 char **a;int i; a = (char * *) malloc (sizeof (char *) * m);//assignment pointer array a[0] = (char *) malloc (sizeof (char) * m * n);//One-time allocation of all spaces for (i=1; i<m; i++) { a[i] = a[i-1] + N;} printf ("%d\n", sizeof (a)),//4, pointer printf ("%d\n", size  Of (a[0]));//4, pointer Free (a[0]); 2.c++ Dynamic allocation of two-dimensional arrays(1) Second dimension known Code-6 char (*a) [n];//pointer to array a = new char[m][n]; printf ("%d\n", sizeof (a)),//4, pointer printf ("%d\n", sizeof (a[  0]));//n, one-dimensional array delete[] A; (2) The first dimension is known Code-7 char* a[m];//pointer array for (int i=0; i<m; i++) a[i] = new Char[n]; printf ("%d\n", sizeof (a)),//4*m, number of pointers  Group printf ("%d\n", sizeof (A[0])),//4, pointer for (i=0; i<m; i++) delete[] a[i]; (3) First dimension known, memory allocated at a time (guaranteed memory continuity) Code-8 char* a[m];//pointer array a[0] = new char[m*n];for (int i=1; i<m; i++) a[i] = a[i-1] + N; printf (   "%d\n", sizeof (a));//4*m, pointer array printf ("%d\n", sizeof (a[0]));//4, Pointer delete[] a[0]; (4) Two dimensions are unknown Code-9 char **a;a = new char* [m];//allocation pointer array for (int i=0; i<m; i++) { A[i] = new char[n];//assigns the array to which each pointer points /c5>} printf ("%d\n", sizeof (a)),//4, pointer printf ("%d\n", sizeof (A[0])),//4, pointer for (i=0; i<m; i++) delete[] a[i];  Delete[] A; (5) Two-dimensional unknown, one-time allocation of memory (guaranteed memory continuity) Code-10 char **a;a = new char* [m];a[0] = new CHAR[M * n];//One-time allocation of all spaces for (int i=1; i<m; i++) { A[i] = A[I-1] + n;//assigns each pointer to the array } printf ("%d\n", sizeof (a)),//4, pointer printf ("%d\n", sizeof (A[0])),//4, Pointer delete[] a[0];  Delete[] A; One more word: New and delete to pay attention to pairing, that is, how many new will have the number of delete, so as to avoid memory leaks! 3. Static two-dimensional arrays are passed as function parametersIf the two-dimensional arrays are dynamically allocated using the above methods, then the corresponding data types can be used as function parameters. Here, the static two-dimensional array is passed as a function parameter, that is, by calling the following method: Int A[2][3];func (a); In C language, it is troublesome to pass a static two-dimensional array as a parameter, it is usually necessary to indicate the length of the second dimension, if the second dimension is not given, it can only be passed as a one-dimensional pointer, then the linear storage property of the two-dimensional array is transformed into the access to the specified element in the function body. First write the test code to verify the correctness of the parameter passing: (1) Given the second dimension length Code-11 void func (int a[][n]) { printf ("%d\n", a[1][2]);} (2) Not given a second dimensional length Code-12 void func (int* a) { printf ("%d\n", a[1 * n + 2]);//compute element position } Note: When using this function, you need to cast the first address of the two-dimensional array to a one-dimensional pointer, func ((int*) a).

Dynamic assignment and parameter transfer of "reprint" two-dimensional array

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.