Gold mine of Visual C++ tricks!
How to Pass Array by Reference?
![]()
We used to use arrays, a lot. But did you ever tried how to pass array by reference to another function? Yes. Its a bit tricky.

![]()
Receiving arrays by reference have special syntax. The arrayname and & symbol should be enclosed in parenthesis. And you should specify the size of array. Have a look at the following code snippet.
// Receive Array by reference.
void GetArray( int (&Array) [10] )
{
}
// Test array by reference.
void CRabbitDlgDlg::TestArray()
{
// Pass array by reference.
int Array[10] = { 0 };
GetArray( Array );
}
![]()
Indeed, you can pass the array as pointer and then use it. But if you ever need to pass an array by reference, then remember this tip.
![]()
Targeted Audience – Beginners.
| Print article | This entry was posted by Jijo Raj on March 15, 2009 at 10:44 am, and is filed under C++, Codeproject. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 11 months ago
What about non sized arrays? Like void ( int (&arrays)[] ); Please specify every details your suggestion. Thansk
about 11 months ago
Hi Marco,
Array should specify their size for being passed as reference. I’m really sorry that i missed that point. Thanks for pointing it out.
Regards,
Jijo.
about 6 months ago
Would this be passed by reference, i have something similar with bool array i use for states in my program. I have never specified the size of what i pass.
// Receive Array by reference.
void GetArray( int Array[] )
{
}
// Test array by reference.
void CRabbitDlgDlg::TestArray()
{
// Pass array by reference.
int Array[10] = { 0 };
GetArray( Array );
}
about 6 months ago
Ok i know it will pass by reference, sorry i meant to ask if it is correct i mean if you say [10] or [] isnt it just the address of the first element ?
about 3 months ago
My understanding is an array cannot be passed as a value, and thus, it is always passed as a reference. It may be passed as a pointed as well.
Anyway, a reference ‘&’ isn’t needed, and the subscript value doesn’t matter.
header: function(int array[]);
call: function(array);