Parameter has a two-dimensional array

Source: Internet
Author: User

The Declaration function has the following void function (int** p), which is intended to pass a two-dimensional array to the parameter. A two-dimensional array is defined, such as int a[1][1], and then the function is called. What's the result? Of course it failed, compiler tip: cannot convert parameter 1 from ' int [1][1] ' to ' int * *, parameter types do not match. I have tried the above process myself, of course, does not match, the type is completely different. Then I thought: if you want to use a two-dimensional array as a formal parameter, what is the function to declare?

Simple point

Array name as formal parameter

void func1 (int array[][]) {}int  main () {    int array[] [ten];    Func1 (array);}

Compile, note that the formal parameter declaration must give the size of the second dimension, or compile.

One-dimensional array pointers as formal parameters

void func2 (int (*parray) [ten]) {}void func2_1 (int// compilation passed, cannot be called, the second dimension is not given {}int  main () {    int array[] [ten];    Func2 (array);}

In fact, the two-dimensional array name is a pointer to a one-dimensional array, so this way of declaring OK. You must specify the length of a one-dimensional array, and if not specified, the function declaration is compiled. But if there is a calling code, there will be no compilation, because no argument type can match int[].

Two-dimensional array references as formal parameters

void func3 (int (&parray) [ten] [ten]) {}int  main () {     int array[] [ten];    FUNC3 (array);}

You must specify a length of two dimensions.

Two-dimensional array pointers as formal parameters

void func4 (int (*parray) [ten] [ten]) {}int  main () {     int array[] [ten];    Func4 (&array);}

You must specify a length of two dimensions

Back to the question that was mentioned earlier in this article: can you accept two-dimensional array arguments using double pointer int** as a formal parameter? The answer is yes, but it's limited. With a double pointer as a formal parameter, the corresponding argument is also a double pointer. In fact, this double pointer actually points to an array of pointers, double pointers are declared in a way that is ideal for passing a two-dimensional array of dynamic (that is, the dimension is a variable) created. How to create a two-dimensional array dynamically? The following programs:

void func5 (intint  n) {  }  int  main () {     int  n=10;    int **a=newint *[n];       for (int i=0; i<n;i++)        a[i]=newint[n];}

Parameter has a two-dimensional array

Related Article

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.