views:

142

answers:

2

I'm working on a program where I try to pass parameters by reference. I'm trying to pass a 2D int array and a 1D char array by reference.

Function prototype:

void foo (int* (&a)[2][2], char* (&b)[4])

Function call:

foo (a, b);

However, when I compile the code with -ansi and -Wall flags on gcc, I get the following errors:

foo.c: At top level:
error: expected ‘)’ before ‘&’ token
error: expected ‘;’, ‘,’ or ‘)’ before ‘char’
foo.c: In function ‘main’:
error: too many arguments to function ‘foo’

I've stripped out the rest of the code of my program and concentrated on the bits which throw up the errors. I've searched around on StackOverflow and tried out different ways to pass the parameters, but none of them seem to work. (I took this way of passing parameters from the discussion on StackOverflow here.)

Could you please tell me where I'm going wrong?

+4  A: 

References are not part of the C language, the syntax you are trying to use only works in C++.

Incidentally, char* (&b)[4] would be a reference to an array of four char* which is a 1d array of char*, not a 1d array of char.

Typically a function taking a 2d array of ints and a 1d array of char would be declared as

void foo(int a[][2], char b[]);

which is the same as:

void foo(int (*a)[2], char *b);
Charles Bailey
So...I can't pass an array by reference in C?
Ankur Banerjee
No, you can have references in any context in C. You also can't pass arrays by value; they always decay to pointers to their first element when passed to functions.
Charles Bailey
In C, its always pass by value. Read more about decaying of arrays to pointers. http://stackoverflow.com/questions/2448204/why-does-this-generate-a-segmentation-fault/2448255#2448255
N 1.1
@Charles and @nvl: Thank you. Changing the function prototype did make it work. I wasn't aware of array decaying in C.
Ankur Banerjee
@Ankur Banerjee: Using pointer-to-array types (or/and reference-to-array types in C++) is actually a way to *prevent* the array type from decaying. So, in the end it is a matter of your intent, a matter of whether you want your array type to decay or not.
AndreyT
+1  A: 

Firstly, as Charles said, C has no references. But normally you can replace references with pointers and get equivalent results through slightly different syntax.

Secondly, your description said that you were trying to pass a 2D int array, but your declaration suggest that it was actually an array of int * pointers. So, what was that supposed to be, an array of int or an array of int *?

Assuming that you wanted an array of int (and an array of char), the variant with pointers-to-array instead of references-to-array might look as follows

void foo(int (*a)[2][2], char (*b)[4]);

Basically the same syntax that you used, but with * instead of &. Inside the function the array elements would be accessed as (*a)[i][j] and (*b)[k].

This function would be called as shown below

int a[2][2];
char b[4];
...
foo(&a, &b);
AndreyT
Thanks, AndreyT. I undestand the difference between the two types of passing arrays now.
Ankur Banerjee