Returns an array pointer

Source: Internet
Author: User

If we want to define a function that returns an array pointer, the dimension of the array must follow the name of the function, but after the function name is the formal parameter list, the formal parameter list should precede the dimension of the array. The function that returns an array pointer is as follows:

Type (*function (parameter_list)) [Dimension]

The declaration of the Func function below does not use a type alias:

int (*func (int i)) [10];

The tail return type can be used in C + + 11

Auto func (intint(*) [ten];

Using Decltype

If we know that the pointer returned by the function is pointing to which array, you can declare the return type using the Delctype keyword, for example, the following function returns a pointer to one of the two known quantity groups according to the difference in parameter I:

int odd[]={1,3,5,7,9}int even[]={0 ,2,4,6,8* ARRPTR (int  i) {      return (i%2)? &odd:&even;  }

Arrptr uses the keyword Decltype to indicate that its return type is a pointer type, and that the pointer refers to an object that is consistent with the type of odd. Because odd is an array, ARRPTR returns a pointer to an array and a pointer containing 5 integers. There is one place to note:

Decltype is not responsible for it. The array type is converted to the corresponding pointer, so the result of the Decltype is an array, and to indicate that the ARRPTR return pointer must also add a * symbol to the function declaration.

Returns an array pointer

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.