Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

General rules of passing/returning reference of array (not pointer) to/from a function?

We can pass reference of an array to a function like:

void f(int (&a)[5]);  int x[5]; f(x);     //okay int y[6]; f(y);     //error - type of y is not `int (&)[5]`. 

Or even better, we can write a function template:

template<size_t N> void f(int (&a)[N]); //N is size of the array!  int x[5]; f(x);     //okay - N becomes 5 int y[6]; f(y);     //okay - N becomes 6 

Now my question is, how to return reference of an array from a function?

I want to return array of folllowing types from a function:

int a[N]; int a[M][N]; int (*a)[N]; int (*a)[M][N]; 

where M and N is known at compile time!

What are general rules for passing and returning compile-time reference of an array to and from a function? How can we pass reference of an array of type int (*a)[M][N] to a function?

EDIT:

Adam commented : int (*a)[N] is not an array, it's a pointer to an array.

Yes. But one dimension is known at compile time! How can we pass this information which is known at compile time, to a function?

like image 267
Nawaz Avatar asked Mar 22 '11 23:03

Nawaz


People also ask

How do you pass references to an array?

Arrays can be passed by reference OR by degrading to a pointer. For example, using char arr[1]; foo(char arr[]). , arr degrades to a pointer; while using char arr[1]; foo(char (&arr)[1]) , arr is passed as a reference. It's notable that the former form is often regarded as ill-formed since the dimension is lost.

Why is it not possible to declare an array of references a pointer to a reference?

According to the C++ standard, an object is a region of storage, and it is not specified if a reference needs storage (Standard §11.3. 2/4). Thus, sizeof does not return the size of a reference, but the size of the referred object. We cannot declare a pointer or a reference to a reference.

How do you return an array by reference in C++?

Return Array from Functions in C++ C++ does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.


1 Answers

If you want to return a reference to an array from a function, the declaration would look like this:

// an array int global[10];  // function returning a reference to an array int (&f())[10] {    return global; } 

The declaration of a function returning a reference to an array looks the same as the declaration of a variable that is a reference to an array - only that the function name is followed by (), which may contain parameter declarations:

int (&variable)[1][2]; int (&functionA())[1][2]; int (&functionB(int param))[1][2]; 

Such declarations can be made much clearer by using a typedef:

typedef int array_t[10];  array_t& f() {    return global; } 

If you want it to get really confusing, you can declare a function that takes a reference to an array and also returns such a reference:

template<int N, int M> int (&f(int (&param)[M][N]))[M][N] {    return param; } 

Pointers to arrays work the same, only that they use * instead of &.

like image 197
sth Avatar answered Oct 23 '22 13:10

sth