Array as reference parameter

I am trying to write a function which swaps two elts of a character array. I want to use the array as a reference parameter , but I'm not sure how the syntax should work.

How should I be laying it out? Including syntax for the prototype (? the bit at the top).

Is this even the problem?
The error I receive is: invalid conversion from `char' to `char*'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using namespace std;

void swapElts (int,int,char[]);

int main ()
{
    char test[5];
    
    test[0] = 'z';
    test[1] = 'a';
    test[2] = 'b';
    test[3] = 'c';
    test[4] = 'd';

swapElts(0,1,test[5]);
cout << test[0] << endl << test[1] << endl ;

system("PAUSE");
return 0;    
}

void swapElts (int a, int b, char& original[])
{
     char temp;
     temp = original[a];
     original[a] = original[b];
     original[b] = temp;
     return;
}


And the error is for line 16 (so maybe my syntax is right and I'm missing something a bit more fundamental?)
your swapElts fucntion is doing the swapping right? Then you must return something and cout it from your main. cannot be void.
you need to #include <iostream> to use cout , remove the brackets from the function call swapElts(0,1,test); (that was passing the address of the 5th element of the array, not the beginning), and the ampersand in the function definition void swapElts (int a, int b, char original[]) by using the ampersand you were trying to re-define the array
Last edited on
swapElts(0,1,test[5]); should be swapElts(0,1,test);
Also, arrays are always passed by reference, so you don't need '&' sign on line 22
void swapElts (int a, int b, char original[])
Awesome, cheers. Did have the iostream, just cut it from the copying by accident.

your swapElts fucntion is doing the swapping right? Then you must return something and cout it from your main. cannot be void.


Is that true? I don't see why it shouldn't be void
void is correct for what you're trying to do. most array functions that are transformers (change stuff in the array) are of type void
i think (also a newb here) the problem is the fact that you've got the function set as void BUT you're asking it to return at the end..you don't tell it what to return, but you're still asking it to do so.. witha void function you don't need line 28 because nothing is returning.

someone PLEASE correct me if i'm wrong because that's how i understand it.
Just a minor nitpick. Arrays are not passed by-reference, they're passed by-pointer (which is a subtle yet very important difference when it comes to understanding what your compiler is doing, and what information it has available inside the function body).

You'll see the difference most clearly whenever you try to do something like figure out the sizeof an array passed by-pointer.

If you want to truly pass an array by-reference (and not by-pointer), you need to think about C++'s "right-left" parsing rule. i.e. int (& foo)[10] means 'foo' ... 'is a reference to' ... 'an array of 10' ... 'int'

If you used int (& foo)[10] as a function argument, then sizeof(foo) would be equal to 10 * sizeof( int )
However if your function looked like void func(int foo[10]) then sizeof(foo) would be equal to sizeof( int* )
The difference between these - by-reference means the function knows how long the array is, by-pointer means that it hasn't got a clue (in the C language, people only have pass-by-pointer available, and often drop in a separate "size" argument alongside the array to use for bounds-checking)


passing an array by-reference can be helpful when mixed with templates - you can let your compiler work out how big your array is for free
1
2
3
4
5
6
7
8
9
10
11
template< int N >
int HowLongIsMyArray( double (& foo)[N] )
{
    return N;
}

int main()
{
     double foo[10];
     std::cout << "My array is " << HowLongIsMyArray( foo ) << " elements long" << std::endl;
}
That trick wouldn't work without the additional (and sadly ugly) reference syntax (And doesn't work with pointers either)
Last edited on
In fact you are trying to swap two objects of type char. So it can be done much simpler

void swap( char &a, char &b )
{
char tmp = a;

a = b;
b = tmp;
}


That is you need not pass the whole array that to swap only two characters.
In your program call to swap would look as

swap( test[9], test[1] );

Then you pass ann array as an argument by value the array is implicitly converted to the pointer to the first element of the array. So the following function declarations are equivalent

1
2
3
void func( char test[5] );
void func( char test[] );
void func( char *test );


If you really need to pass array by reference you should declare function as

void func( char ( *test )[5] );

However as I have pointed already in your program it is better to pass only two array elements and declare swap as a function that takes two references to char.
Topic archived. No new replies allowed.