473,394 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Passing string types to a function

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
4 49217
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
13
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
13
by: anonymike | last post by:
Hello, I started working with the ObjectDataSource today. I have the select, and have been working on getting the update method to work. Here is the asp code for my Data source: ...
3
by: sd2004 | last post by:
I am still learning, could someone show/explain to me how to fix the error. I can see it is being wrong but do not know how to fix. could you also recommend a book that I can ref. to ?...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
8
by: =?Utf-8?B?UmF2aQ==?= | last post by:
Hi, I'm trying to pass values of different data-types to a web-service. I thought it would be easier to box these values and pass them as a System.object parameter, like public void...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
18
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.