How to Pass Array by Reference?

15 03 2009


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.

arraybyreference


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.

Advertisements