Stupid C++ question.

notfred

Lifer
Feb 12, 2001
38,241
4
0
I havent written any C at all in a while, been writing perl. anyway - how do I pass an array by reference to a function?

could you write a simple function call and function prototype just showing how to pass an already declared array?

here's our array: int array[10];

and a function: void recieve_array(????);


and the function call: recieve_array(????);

can someone fill in the question marks for me? I feel stupid that I can't remember this.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
I think that arrays are always passed by reference in C. As for the syntax:


void receive_array(int array[])
{
}

OR

void receive_array2(int *array)
{
}


/* How to call the function receive_array */
void myfunction(void)
{
int array[10];
receive_array(array);
}

 

GL

Diamond Member
Oct 9, 1999
4,547
0
0
I think what you want to do is a pass a pointer to the array as a function parameter.

For example if you have an array of chars, you'd pass a char pointer to element 0 of that array, as the function parameter.
 

blahblah99

Platinum Member
Oct 10, 2000
2,689
0
0
signh is correct, but if you pass by reference,

you need to make sure you increment or decrement the pointer in order to access other elements of the array.