How to pass an array of strings by reference?`
Hello,
I'm trying to pass an array of strings to a function by reference and can't seem to figure out how to do it. Here is what I'm tring to do.
#include <string>
void goodbye(string &ra[]);
int main();
{
int lines = 3;
string a[lines];
string a[0] = "Hello";
string a[1] = "there";
goodbye(a);
return 0;
}
void goodbye(string &ra[])
{
ra[0] = "goodbye";
ra[1] = "";
}
I know this isn't correct, but this is basically what I'm trying to do. Can anyone patch up this so that it would work or point me to a page with an explanation/tutorial? As of yet, google has failed me on this one.
Thanks in advance,
James1
This should answer the actual question
Code:
#include <iostream>
#include <string>
using namespace std;
void pass_by_reference(string (&ref)[10])
{
ref[0] = "This is string one";
// this is how you take a reference for an array
}
int main()
{
string str[10];
pass_by_reference(str);
return 0;
}
to make it simple....
int x[10]; // if you wanted a reference to this array of integers
int (&ref)[10] = x; // this is how you take the reference of the array x