C ++ array as return value

Source: Internet
Author: User

Arrays and pointers representing arrays describe the same problem in two forms. They are actually the same.
In C ++, it is clear in the function section that arrays in the form parameters are treated as pointers directly.
"No way to return an array at all" is correct in a narrow sense. This is to indicate the author's error. However, in a broad sense, it is incorrect because the pointer is returned, returns an array in C ++.
Which book tells you that "boundary" is an array. Which of the following textbooks does it mean there is no "field" when the function returns an array? In void cube (int * n, int num), what is the int num that you call "bounds?

 

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

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, the pointer to be returned by a custom function in the form of (* P) [N, how should I declare it? 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; // you can point
P1 = a (); // This row is incorrect

}
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 returns the address of the first line of the Two-dimensional array. 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; // here, how to operate the pointer P is only to operate 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 the two-dimensional array in C ++ 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.

What happened in my post on the 30th floor is also incredible. I can't directly use a two-dimensional pointer to point to the first address of a two-dimensional array, the function that uses a two-dimensional pointer to indicate an address and returns the address is yes.
. In short, I think that in C ++, when the function cannot return an array, the pointer of one dimension or more corresponding dimensions cannot be used to point to the array of corresponding dimensions, this is inconvenient.
I have never seen any pointer used in 3D arrays.

 

When passing an array to a function, you must add a num variable to get 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 it was not solved until c99 emerged and the variable-length array can be declared. Unfortunately, Vc has not supported the variable-length array. 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

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.