GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Jul-2005, 19:29
Atomical Atomical is offline
New Member
 
Join Date: Jul 2005
Posts: 25
Atomical has a little shameless behaviour in the past

strings and referencing


I'm using the string library and I thought that because strings were passed by reference that I didn't need pointers. Right now the program isn't sending anything to standard output like it should be.

CPP / C++ / C Code:
#include <iostream>
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <unistd.h> 
#include <assert.h> 
#include <cstring>
#include <ctype.h>

using namespace std;

void test ( string s );


int main (int argc, char * const argv[]) {

string s;
test(s);
cout << s;

return 1;

}


void test ( string s ){
s = "asdfadfs";
}
Last edited by LuciWiz : 04-Jul-2005 at 23:39. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 04-Jul-2005, 20:46
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,108
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
First off, you will be more likely to get help by reading the how to post requests thread which will tell you how to use code tags. It gives some good pointers on including information as well.

Quote:
Originally Posted by Atomical
I'm using the string library and I thought that because strings were passed by reference that I didn't need pointers. Right now the program isn't sending anything to standard output like it should be.

Your problem is that you are not passing by reference you are passing by value. When s ends up in your test routine it sets the local variable s to "asdfadfs". The variable s in main is a different variable of the same name (with different scope if you will) and never gets set to anything.

Now, as you will see I will put the code between the code tags.
Code:
CPP / C++ / C Code:
// code will be here

CPP / C++ / C Code:
void test ( string s ){
s = "asdfadfs";
}

In order to charnge this change your function just a bit.
CPP / C++ / C Code:
void test ( string& s ){

Now you will be operating on the s from main when you are in test().

Is there a reason for all the excess in your includes? For your simple example you just need the following.

CPP / C++ / C Code:
#include <iostream>
#include <string>

using std::cout;
using std::string;
// using namespace std;

As you see, I commented out the using namespace std; and just used the using std::cout and string. They accomplish the same end result but are more limiting in what they actually include. There are quite a few posts that a search of the forum can dig up. If you don't like my method you could just go back to the using namespace std way and get rid of the using statement.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #3  
Old 05-Jul-2005, 07:44
Atomical Atomical is offline
New Member
 
Join Date: Jul 2005
Posts: 25
Atomical has a little shameless behaviour in the past
I should I be using the string library or should I be using character arrays? Is one faster than the other? Also, I put in the code you suggested and got the same result.

CPP / C++ / C Code:
#include <cstring>
#include <ctype.h>
#include <iostream>

using std::cout;
using std::string;


void test ( string s );


int main (int argc, char * const argv[]) {

string s;
test(s);
cout << s;

return 1;

}


void test ( string s ){
s = "asdfadfs";
}
  #4  
Old 05-Jul-2005, 08:01
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about
Quote:
Originally Posted by Atomical
I should I be using the string library or should I be using character arrays? Is one faster than the other?
That question can bring a lot of debate. But I would say use string. They are easier to use and have many helpful member functions.

Quote:
Originally Posted by Atomical
Also, I put in the code you suggested and got the same result.

CPP / C++ / C Code:
#include <cstring>
#include <ctype.h>
#include <iostream>

using std::cout;
using std::string;


void test ( string s );


int main (int argc, char * const argv[]) {

string s;
test(s);
cout << s;

return 1;

}


void test ( string s ){
s = "asdfadfs";
}
That is not what was suggested. Add the "&".
CPP / C++ / C Code:
//#include <cstring>//string not here
#include <string>//string here
//#include <ctype.h>//old
#include <cctype>//standard
#include <iostream>

using namespace std;

void test ( string& s );

int main () {

	string s;
	test(s);
	cout << s;

	return 1;

}

void test ( string& s )
{
	s = "asdfadfs";
}
Also
Header Files
http://msdn.microsoft.com/library/de...eaders_stl.asp

<string>
http://msdn.microsoft.com/library/de...ing_header.asp

is not the same as
<cstring>
http://msdn.microsoft.com/library/de...ing_header.asp
 

Recent GIDBlogPrepping for deployment by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:22.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.