c++--functions that return array pointers

Source: Internet
Author: User
Tags aliases

Basics: Arrays cannot be copied, functions cannot return arrays, only pointers or references to arrays are returned.

A method that defines a function that returns an array pointer , taking as an example a function that receives a parameter as a reference to an array containing 10 integral elements and a pointer to an array containing 10 integral elements.

Law one: (do not use aliases)

int(*getcopyarr (int(&arr) [Ten]))[Ten]{    int(*n) [Ten] = (int(*) [Ten])New int[Ten];  for(inti =0; I <Ten; i++)        (*N) [I] =Arr[i]; returnN;}//Note: The function returns an array allocated in the heap, and if it does not release memory after the array is obtained, it will cause a memory leak, which is only as an example .

Law II:

// tail return type (c++11 standard)auto getCopyArr2 (int(&arr) [ten]),int(*) [ ]{    return  Getarr (arr);}

Fahsarm

// Use type aliases (1) int arrt[];arrt* GETCOPYARR3 (int(&arr) [ten]) {     return Getarr (arr);}

Law IV:

// Use type aliases (2) (C++11 standard) using int [];arrt2* GETCOPYARR4 (int(&arr) [ten]) {    return Getarr (arr);}

A method that declares a function pointer to a function that returns an array pointer :

int (* (*GETCA) (int(&) [ten])) [ten= Getarr;
// A function pointer int (*fun) (int, int) that resembles the int sum (int a, int b) function // .

Reference blog post and "C + + Primer 5th Edition"

---------------------------------------------------------------------------------------

Finally, here are two examples of "refreshing the Brain":

// declares a pointer to an array of 10 elements, where each element is a function pointer, and the return value of the function is int, and the argument is int* int (* (*p) [ten]) (int *) // or int (* (*p[)) (int *) // Source
// Defines a pointer to an array of 4 elements, each of which is a pointer to a function whose reference returns a pointer to an array of 10 elements with a value of 10 elements int (* (*getca[4]) (int(&) [ten])) [ten];

c++--functions that return array pointers

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.