6.3.3. Returning a Pointer to an Array

Because we cannot copy an array, a function cannot return an array. However, a function can return a pointer or a reference to an array (§ 3.5.1, p. 114). Unfortunately, the syntax used to define functions that return pointers or references to arrays can be intimidating. Fortunately, there are ways to simplify such declarations. The most straightforward way is to use a type alias (§ 2.5.1, p. 67):

typedef int arrT[10];  // arrT is a synonym for the type array of ten intsusing arrT = int[10];  // equivalent declaration of arrT; see § 2.5.1 (p. 68)arrT* func(int i);     // func returns a pointer to an array of ten ints

Here arrT is a synonym for an array of ten ints. Because we cannot return an array, we define ...

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.