Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing by reference 3-Dim Fixed length array

Can anyone hint on how to pass by reference an array of the kind

int array[2][3][4];

so that I may save his pointer in order to use and modify the array? Like, if I were speaking about a single integer:

// Scope 1
int a = 3;
increment(&a);
// End Scope 1

// Scope 2
int *pa;
void increment(int *tpa) { 
  pa = tpa; *pa++; 
}
// End Scope 2

Thanks a lot and best regards.

like image 468
JoeSlav Avatar asked Feb 11 '26 22:02

JoeSlav


2 Answers

If you really want to pass the array by reference, you can do so:

void f(int (&a)[2][3][4]) { }

In C, which doesn't have references, you can pass the array by pointer (this works in C++ too, of course):

void f(int (*a)[2][3][4]) { }
like image 165
James McNellis Avatar answered Feb 13 '26 13:02

James McNellis


C++:

void f(int (&array)[2][3][4])
{
}

C: There are no references in C

Note that no matter how you pass the array, via reference or not, the array is not going to be copied, so you'll get the original pointer. You can pass this array also like this:

void f(int array[][3][4])
{
}
like image 43
Armen Tsirunyan Avatar answered Feb 13 '26 14:02

Armen Tsirunyan