How does a function in C ++ return a two-dimensional array. How does a pointer point to a two-dimensional array?

Source: Internet
Author: User

Arrays and pointers representing arrays describe the same problem in two forms. They are actually the same.
C ++
In the function section, it is clear that the array in the form parameter will be treated as a pointer directly.
"
There is no way to return an array"
In a narrow sense, this is to indicate the author's error; but in a broad sense, it is incorrect, because the returned pointer is in C ++
Returns an array.

The upstairs is even more nonsense. Which book will tell you"
Bounded"
It is an array. you can create your own words. Which of the following textbooks does not show how functions return arrays?
"
? Void cube (int *
N, int num)
The Int num in
Not what you call"
"
What is it?

 

========================================================== =

We have figured out how pointers refer to two-dimensional arrays. This should be the case.
Void main ()
{
Int A [10] [10] = {0}, (* P) [10];
P =;
}

This declares a two-dimensional pointer pointing to a two-dimensional array.

Then the new problem arises. How does a custom function return a two-dimensional pointer, that is, how does a custom function return a shape such as (* P) [N]?
How should I declare the pointer? All the experts upstairs have said that the function can only return pointers, but when I try to return a two-dimensional pointer. The compiler still reports an error!
Int * ();
Void main ()
{
Int B [10] [10] = {10}, (* P1) [10];


P1 = B ;//
Can point
P1 = ();//
This line is wrong

}
Int * ()
{
INT (* P) [10], B [10] [10] = {2 };
P = B;
Return P;
}

What should I do to return a two-dimensional array ????!!!

========================================================== =

If so, F
Only the address of the first line of the Two-dimensional array is returned. You cannot operate the elements of the second row of the array in the main function. All I can do is operate the elements of the first line, for example:
# Include <iostream>
Using namespace STD;
Int * F ()
{
Int arr [2] [3] = {1, 2, 3 },
{7, 8, 9 }};
Return arr [0];
}

Int main ()
{
Int * P;
P = f ();

Cout <* P <Endl ;//
How to operate pointer P
All operations are performed on the first line of the Two-dimensional array.
System ("pause ");
Return 0;
}

However, the elements in the second row cannot be operated.

========================================================== ========

The array is linear and the starting address is obtained. Isn't it easy to do it later?

========================================================== ========

After reading your post, I think C ++
The array above two dimensions is a chicken rib.

A one-dimensional array is good, because the pointer pointing to a one-dimensional array can easily operate on one-dimensional arrays, And the pointer to a two-dimensional array is purely used, at least I know that the function cannot return a two-dimensional array (*) []
Pointer type.

In my 30
The post in the building is also incredible. It is impossible to directly use a two-dimensional pointer to point to the first address of a two-dimensional array with an address, the function that uses a two-dimensional pointer to indicate an address and returns the address is yes.
. In short, I think
When the function cannot return an array, it is inconvenient to point the pointer to an array with the corresponding dimension above one dimension.

I have never seen any pointer used in 3D arrays.

 

Add a num when passing the array to the function.
The variable is used to obtain the array size, that is, the boundary...
Because the one-dimensional array degrades to a pointer ..
It can be moved freely in linear memory ....
As a result, the boundary is lost, and the boundary may be crossed...
So + num ......
The one with me last time ....
I can only

========================================================== ==========

The problem with the two-dimensional array is that until c99
The appearance of, allow the declaration of Variable Length array to be fully resolved, unfortunately VC
Variable-length array is not supported. So if you want to use the most basic data type, you should use a one-dimensional array as two-dimensional. If you want to write beautifully, write your own array template.

========================================================== ==========

 

Source: http://bbs.bccn.net/viewthread.php? Tid = 196439 & extra = & page = 1

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.