I am writing a function that return a string, however, whenever I compile it, error occurs, it says: "warning: address of local variable
'aString' returned", how come?
I am writing a function that return a string, however, whenever I compile it, error occurs, it says: "warning: address of local variable
'aString' returned", how come?
I forgot to post an example:
#include <iostream.h>
#include <string.h>
char* getString() {
char x[100];
strcpy(x, "How are you?");
return(x);
}
int main () {
cout << getString() << endl;
return 0;
}
When compiling the above program, an error message will echo:
" In function 'char * getString()' :
warning: address of local variable 'x' returned "
How can I solve this problem?
Thanks a lot.
try
string MyFunction()
rather than
int MyFunction()
not sure but it might help
James G. Flewelling
Rgistered Linux User #327359
Athabasca University Student (BSc. CIS)
http://catb.org/~esr/faqs/smart-questions.html
http://catb.org/jargon/
http://www.ebb.org/ungeek
---GEEK CODE---
Version: 3.12
GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
----/GEEK CODE----
upd: 2005-02-11
Local variables go out of scope when you leave the function, this means that the memory they occupied is now available to anything running on the computer that needs it and usually when you try to read the variable outside of the function you'll get garbage. The easiest solution is to pass a buffer to the function instead of declaring it in the function.
-PreludeCode:#include <iostream.h> #include <string.h> char * getString( char * buf) { strcpy(buf, "How are you?"); return buf; } int main () { char ret[100]; getString( ret ); cout << ret << endl; return 0; }
My best code is written with the delete key.
oops posted before i read your code anyway try this following it might help;
--cut--
actually i dont think that works.. me better off not confusing you more.. i will go see if i can figure something out for you... if anyone else has a real solution tho dont hesitate to help..
Last edited by Cobras2; 03-13-2002 at 02:03 PM.
James G. Flewelling
Rgistered Linux User #327359
Athabasca University Student (BSc. CIS)
http://catb.org/~esr/faqs/smart-questions.html
http://catb.org/jargon/
http://www.ebb.org/ungeek
---GEEK CODE---
Version: 3.12
GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
----/GEEK CODE----
upd: 2005-02-11
I guess some of you may say, " declare to char x[100] as a global variable rather than declaring it inside getString()"
It works!!!
However, I am actually writing a class. Any variable declaring outside the member function would not be suitable. I want all variable to be "private". However, if I declare char x[100] in the private part of the class, how can I change the content of it inside my member function which is located in the public part?
Well, it's nice. But it won't work in my case because my getString() is one of my member function of a class. My teacher require it to be parameterless.Originally posted by Prelude
Local variables go out of scope when you leave the function, this means that the memory they occupied is now available to anything running on the computer that needs it and usually when you try to read the variable outside of the function you'll get garbage. The easiest solution is to pass a buffer to the function instead of declaring it in the function.
-PreludeCode:#include <iostream.h> #include <string.h> char * getString( char * buf) { strcpy(buf, "How are you?"); return buf; } int main () { char ret[100]; getString( ret ); cout << ret << endl; return 0; }
Correct me if I am wrong, but public member functions of a class have access to private variables stored in that class (or rather that object of that clas.) in order to get a working compilable code you would actually have to declare the class as a whole.
--edit--
how about this? it works :)
Code:#include <iostream.h> #include <string.h> class MyClass { private: char x[100]; public: char* getString() { strcpy(x, "How are you?"); return x; } }; int main () { MyClass test; cout << test.getString() << endl; return 0; }
Last edited by Cobras2; 03-13-2002 at 02:14 PM.
James G. Flewelling
Rgistered Linux User #327359
Athabasca University Student (BSc. CIS)
http://catb.org/~esr/faqs/smart-questions.html
http://catb.org/jargon/
http://www.ebb.org/ungeek
---GEEK CODE---
Version: 3.12
GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
----/GEEK CODE----
upd: 2005-02-11
I whipped up an example similar to Corbras2, and it worked also.