Cause Analysis of array not returned in C ++

Source: Internet
Author: User

Arrays cannot be returned in C ++! That is to say, in C ++, arrays cannot be treated as the return value of the function; otherwise, an error is returned during compilation!
If you want to return the data, you can return the data in the pointer format ~
Now you have to ask, in C ++, the array can be passed to the function as a real parameter. Why cannot the array be returned ?!!!
In C ++, when an array is passed to a function as a real parameter, the real parameter array is not copied to the form parameter, but is converted to a pointer, then, the "That Pointer" is initialized with the real parameter. That is to say, the array parameter variable points to the same memory space as the real parameter variable (which can explain why sizeof (form parameter array) in the function) it is always equal to 4 ", because the form parameter is converted into a pointer! Sizeof (form parameter array) is equivalent to sizeof (a pointer )!), That is to say, you can pass an array real parameter to the function, and then modify the value in the array through the form parameter in the function, then the value in the real parameter group outside the function is modified! Amazing, right ?!
See: C ++ primer Chinese (Fourth Edition) P206-P208

Since the pointer is passed, it is meaningless to return an array created in the function ~ Because the array created inside the function will be destructed after the function is executed, even if it is returned, the data will be lost!
If you still don't give up and argue like me, I can pass in an external array created by a function, and then I will return this external array. Isn't that true? In this case, the structure will not be analyzed!
Even so, you need to know that the array outside the function does not need to be returned, you can directly modify its value inside the function.

To sum up, weigh the balance, so the C ++ medium pressure root will not let the returned 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.