Basic knowledge of C + +

Source: Internet
Author: User

1

In C + +, arrays are not allowed as return values for functions.

int [] someFunction (); Illegal

To implement a function that returns an array, returns a pointer to the type of the corresponding array

you must return a pointer to the array base type and has the pointer point to the array. So, the function declaration would is as follows: 
 int* someFunction ();//legal

int* doubler (int a[], int size)
{
int* temp = new Int[size];
for (int i =0; i < size; i++)
Temp[i] = 2*a[i];
return temp;
}

Call

int a[] = {1, 2, 3, 4, 5};
int* b;
b = Doubler (A, 5);

Delete[] B


Basic knowledge of C + +

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.