By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Passing string types to a function

P: n/a
Help me get my mind around passing string types to a function.

I need to have the function modify the string types and get them back.
Normaly I would just return the modified string but I need to modify about
five different strings.

I thought that strings were passed by reference in C/C++ but when I pass
the strings in they remain unaltered in the calling function. I did test
the function being called and they are being althered there.

Can someone please show me the syntax that I need.

i.e.

void foo(string s, string s2)
{

}

int main(int argc, char *argv[])
{
string a, b;
foo(a, b);
return 0
}

Or whatever is correct (the above doesn't work.)

Thanks.
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
void foo(string& s, string& s2)
{

}

--
Derek
"Suddn" <Bo***@NoSpam.net> wrote in message
news:pa****************************@NoSpam.net...
Help me get my mind around passing string types to a function.

I need to have the function modify the string types and get them back.
Normaly I would just return the modified string but I need to modify about
five different strings.

I thought that strings were passed by reference in C/C++ but when I pass
the strings in they remain unaltered in the calling function. I did test
the function being called and they are being althered there.

Can someone please show me the syntax that I need.

i.e.

void foo(string s, string s2)
{

}

int main(int argc, char *argv[])
{
string a, b;
foo(a, b);
return 0
}

Or whatever is correct (the above doesn't work.)

Thanks.

Jul 19 '05 #2

P: n/a
Suddn wrote:
...
I thought that strings were passed by reference in C/C++ but when I pass
the strings in they remain unaltered in the calling function. I did test
the function being called and they are being althered there.
...
The compiler will pass the strings the way you asked it to pass them. If
you direct the compiler to pass them by value, it will pass them by
value. The same applies to passing by reference.
void foo(string s, string s2)


This function will receive its arguments by value. In order to receive
them by reference you have to declare it as follows

void foo(string& s, string& s2)

--
Best regards,
Andrey Tarasevich

Jul 19 '05 #3

P: n/a
In article <pa****************************@NoSpam.net>,
Suddn <Bo***@NoSpam.net> wrote:

I thought that strings were passed by reference in C/C++
That refers to C-style strings which are just arrays of char. Arrays in
general are passed implicitly by reference.

C++ style strings behave like "normal" data types as far as passing them
to functions is returned. If you want to change the value inside the
function and have that change communicated back to the calling function,
pass it by reference, or by pointer. The same applies for vectors, by the
way, if you're using them instead of arrays (which you probably should).
void foo(string s, string s2)
{

}


void foo (string& s string& s2)

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #4

P: n/a

"Suddn" <Bo***@NoSpam.net> wrote in message
news:pa****************************@NoSpam.net...

I thought that strings were passed by reference in C/C++ but when I pass
the strings in they remain unaltered in the calling function. I did test
the function being called and they are being althered there.

Can someone please show me the syntax that I need.

i.e.

void foo(string s, string s2)
{

}

int main(int argc, char *argv[])
{
string a, b;
foo(a, b);
return 0
}


void foo(string& s, string& s2)
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.