Friday, July 31, 2009

C++: How do you edit a character string in a different function than the one you declared it in?

The following code is C++:








#include "stdafx.h"


#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


#include %26lt;string%26gt;


using namespace std;





int rewriteString(char *str)


{


str = "I want to output this!";


cout %26lt;%26lt; str;


return 1;


}





void main()


{


int meaninglessInteger = 0;


char str[] = "Why am I outputting this, instead of what I want to output?";


meaninglessInteger= rewriteString(str);


cout %26lt;%26lt; str;


system("PAUSE");


}





******* ******* ******* ******* ******* ******* ******* ******* *******


The above code doesn't work. I wondered if someone could help me understand what I need to change to accomplish what I'm trying to do.


I hope the code is fairly self-explanitory: I want to declare a character string in main() and then use a function called rewriteString() to change the message contained in that character string.





What's the correct way to do this? Any help is appreciated greatly!

C++: How do you edit a character string in a different function than the one you declared it in?
just my two cents ;)





int rewriteString(char *str)


{


strcpy(str,"output");


cout %26lt;%26lt; str;


return 1;


}
Reply:Okay. There is actually a very simple explanation to your answer. Once i tell you, you'll wonder why you didnt think of this in the first place.





Now, from basic C++ programming, you'd know this simple example:





void foo( int f ) { f = 25; }


void main() { int a = 10; foo(a); cout %26lt;%26lt; a; }





Now what do you think will be the output. Correct, it will be 10.


Why? Because the argument f to function foo was "passed by value" variable 'a' never changes its value. Now... just extending this to character strings.





void foo( char * f ) { f = "please please change"; }


void main() { char * t= "dont want to change"; foo(t); cout %26lt;%26lt; t; }


Now, :) i think by now you'd be saying "aaah, hmm... ". So, the solution. If you havent caught on, i'll tell you (this is Yahoo! Answers after all).. you've got two options


1. Pass parameter by reference ... void foo( char *%26amp; f )


2. Pass a pointer to the argument... void foo( char ** f )





I think you can figure out the rest. Due to the lack of availability of a compiler to check my analysis. I might be mistaken over char *%26amp; f... it might be, char %26amp;* f . But i dont think so. Sorry, but you'll have to check that.





Thanks.
Reply:I'm sorry if this is not what you want but I find it easier to just do this...





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


#include %26lt;string%26gt;


using namespace std;





char *rewriteString(char *str)


{


char * random = "theText";


return random;


}





int main() {


char * str = new char[100];


str = "Why am I outputting this, instead of what I want to output?";


str = rewriteString(str);


cout %26lt;%26lt; str;


system("PAUSE");


return 0;


}





===


I have no clue why you have the meaninglessInteger variable


No comments:

Post a Comment