I'm trying to return two integers from a function, but I can't get it to work. Actually I want to do this for two chars (returning two filenames) but I thought I should try it with integers first.
I'm sorry if it's a trivial mistake, but I can't see where I'm going wrong and I'd really appreciate some advice (I'm new to c++):
#include <iostream>
usingnamespace std;
// Declare function prototype as the address of two integers
void ReadInput(int& a, int& b);
int main()
{
int a, b;
// call function, passing the two variables
ReadInput(a,b);
cout << a << " " << b << endl;
return 0;
}
// declare function, taking as input the address of the integers
void ReadInput(int& a, int& b)
{
// assign values at the address of the variables.
*a = 5; // error at this line
*b = 4; // error at this line
}
Since you are passing by reference (this is slightly different than passing by pointer), you don't use the *. The references are "already dereferenced" you could say.