473,763 Members | 7,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 49246
void foo(string& s, string& s2)
{

}

--
Derek
"Suddn" <Bo***@NoSpam.n et> wrote in message
news:pa******** *************** *****@NoSpam.ne t...
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.n et> 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*******@pres by.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #4

"Suddn" <Bo***@NoSpam.n et> wrote in message
news:pa******** *************** *****@NoSpam.ne t...

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
10179
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 code... TCHAR myArray; DoStuff(myArray);
13
2797
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
8230
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: <asp:ObjectDataSource ID="dsourceApps" runat="server" SelectMethod="getAllApplications" TypeName="AppMgr.AppManager" UpdateMethod="updateAppHdr" ConflictDetection="CompareAllValues"
3
2381
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 ? ////////////////// ERROR MESSAGE ///////////////////// bash-2.05b$ g++ test5c.cpp test5c.cpp: In function `int main()': test5c.cpp:36: error: invalid initialization of reference of type
12
2684
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
3306
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 object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
8
8903
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 webmethod1(object a) where a can be of type string, DateTime, float, etc..
17
7253
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 to show the array data to the end user. Can I do that? How?
18
3269
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
9564
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9387
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9823
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.