Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an array by reference in C?

How can I pass an array of structs by reference in C?

As an example:

struct Coordinate {    int X;    int Y; }; SomeMethod(Coordinate *Coordinates[]){    //Do Something with the array } int main(){     Coordinate Coordinates[10];    SomeMethod(&Coordinates); } 
like image 450
Hannoun Yassir Avatar asked Jul 09 '09 23:07

Hannoun Yassir


People also ask

Can you pass an array by reference in C?

The C language does not support pass by reference of any type. The closest equivalent is to pass a pointer to the type.

What is passing an array by reference?

If we pass the address of an array while calling a function, then this is called function call by reference. The function declaration should have a pointer as a parameter to receive the passed address, when we pass an address as an argument.

How can you reference array in C?

In your first function() what gets passed is the address of the array's first element, and the function body dereferences that. Infact, the compiler is treating the function prototype as this: void function(int* array /*you wrote int array[]*/){ array[0] = 4; array[1] = 5; array[2] = 6; } function(&array[0]);

How arrays are passed to functions in C?

To pass an entire array to a function, only the name of the array is passed as an argument. result = calculateSum(num); However, notice the use of [] in the function definition. This informs the compiler that you are passing a one-dimensional array to the function.


1 Answers

In C arrays are passed as a pointer to the first element. They are the only element that is not really passed by value (the pointer is passed by value, but the array is not copied). That allows the called function to modify the contents.

void reset( int *array, int size) {    memset(array,0,size * sizeof(*array)); } int main() {    int array[10];    reset( array, 10 ); // sets all elements to 0 } 

Now, if what you want is changing the array itself (number of elements...) you cannot do it with stack or global arrays, only with dynamically allocated memory in the heap. In that case, if you want to change the pointer you must pass a pointer to it:

void resize( int **p, int size ) {    free( *p );    *p = malloc( size * sizeof(int) ); } int main() {    int *p = malloc( 10 * sizeof(int) );    resize( &p, 20 ); } 

In the question edit you ask specifically about passing an array of structs. You have two solutions there: declare a typedef, or make explicit that you are passing an struct:

struct Coordinate {    int x;    int y; }; void f( struct Coordinate coordinates[], int size ); typedef struct Coordinate Coordinate;  // generate a type alias 'Coordinate' that is equivalent to struct Coordinate void g( Coordinate coordinates[], int size ); // uses typedef'ed Coordinate 

You can typedef the type as you declare it (and it is a common idiom in C):

typedef struct Coordinate {    int x;    int y; } Coordinate; 
like image 109
David Rodríguez - dribeas Avatar answered Sep 25 '22 02:09

David Rodríguez - dribeas