Passing two-dimensional arrays in C Language

Source: Internet
Author: User

A long time ago, when I learned C language, when I put, data type, constant variables, statement control, functions, pointers, struct, custom data types, and some simple recursion, after the study, I thought I could pass the C language .. I saw a C-language book that day. I can't believe it. There are nearly 1000 pages of books .. i'm surprised that the thick books can only be seen in books with many languages such as C ++ and Java ..

I finally concluded that my C language has not passed ,,,

 

I encountered a problem of passing two-dimensional arrays in C language... I didn't think of it before... I tried to make a... it wasn't just what I thought ..

I started to write this:

 

# Include <iostream>

Using namespace STD;

 

Typedef vector <int> CN;

 

Void show (INT N1, int N2, int * P [])

{

For (INT I = 0; I <N1; I ++)

{

For (Int J = 0; j <N2; j ++)

{

P [I] [J] ++;

Cout <p [I] [J] <"";

}

 

Cout <Endl;

}

 

}

 

Int main ()

{

Int A [3] [3] = {1, 2, 3}, {4, 5, 6}, {7, 8, 9 }};

Show (3, 3, );

Return 0;

}

 

The result is not what I think: An error like this is prompted...

 

INT (*) [] type cannot be assigned to, int **....

 

This makes me very surprised. Are these two not the same class shapes .//

 

 

 

The compiler certainly won't go wrong. It's just my own problem.

 

I immediately realized that the name of a two-dimensional array is not a simple second-level pointer...

 

Then I tried to do this, int ** P =;

 

The same mistakes prove that my ideas are correct ..

 

The procedures for my correction are as follows:

 

 

# Include <iostream>

# Include <vector>

Using namespace STD;

Typedef vector <int> CN;

 

Void show (INT N1, int N2, int ** P)

{

For (INT I = 0; I <N1; I ++)

{

For (Int J = 0; j <N2; j ++)

{

P [I] [J] ++;

Cout <p [I] [J] <"";

}

 

Cout <Endl;

}

 

}

 

Int main ()

{

Int **;

A = new int * [3];

For (int K = 0; k <3; k ++)

A [k] = new int [4];

 

For (INT I = 0; I <3; I ++)

For (Int J = 0; j <4; j ++)

A [I] [J] = 3;

 

For (INT I = 0; I <3; I ++)

{

For (Int J = 0; j <4; j ++)

Cout <A [I] [J];

Cout <Endl;

}

Show (3, 3, );

Return 0;

}

 

 

Change the array to dynamic application, and solve the problem ....

 

What does this mean .:

 

Apply directly. The structure of the memory space is different from that of the dynamic review array...

 

For directly applied arrays, the second-level pointer is not allowed to find the storage address of all its elements...

 

As for its space, it is directly applied continuously ..

 

 

This may also be the case when some compilers prohibit the transfer of two-dimensional arrays... it is a defect in the Design of Two-dimensional arrays in C language (C ++...

 

Therefore, in the C ++ program design, we should avoid doing this... we can use a lot of vector and other good data structures to replace it ....

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.