First, the array cannot be copied, so the function cannot return an array, but it can return an array of pointers or references, and there are several ways to return an array of pointers:
1. Using Type aliases
#include <iostream>using namespacestd;intb[Ten] = {1,2,3,4,5,6,7,8,9,0};typedefinttype[Ten];//or using Type=int[10],type is a type aliasType *sum (int(&a) [Ten]) { return&a;//You cannot return a pointer or reference to a local variable, so the function parameter mining &}intMainintargcChar*argv[]) { int(*p) [Ten] =sum (b); int*a = *p; for(inti =0; I <Ten; i++) cout<< * (A + i) <<Endl;; return 1;}
2. Direct definition
#include <iostream>using namespacestd;intb[Ten] = {1,2,3,4,5,6,7,8,9,0 }; int(*sum (int(&a) [Ten]))[Ten]//the dimensions of the array are placed last { return&A;} intMainintargcChar*argv[]) { int(*p) [Ten] =sum (b); int*a = *p; for(inti =0; I <Ten; i++) cout<< *a++<<Endl;; return 1; }
3. Use Auto
#include <iostream>using namespacestd;intb[Ten] = {1,2,3,4,5,6,7,8,9,0};auto sum (int(&a) [Ten])int(*) [Ten]//Auto Fun (formal parameter list)->int (*) [10], writes the true type behind{ return&A;}intMainintargcChar*argv[]) { int(*p) [Ten] =sum (b); int*a = *p; for(inti =0; I <Ten; i++) cout<< *a++<<Endl;; return 1;}
4. Using Decltype
#include <iostream>using namespacestd;intb[Ten] = {1,2,3,4,5,6,7,8,9,0};d Ecltype (b)*sum (int(&a) [Ten])//using Decltype{ return&A;}intMainintargcChar*argv[]) { int(*p) [Ten] =sum (b); int*a = *p; for(inti =0; I <Ten; i++) cout<< *a++<<Endl;; return 1;}
Functions that return function pointers