How to write a function that returns a string?

This is a discussion on How to write a function that returns a string? within the C++ Programming forums, part of the General Programming Boards category; I am writing a function that return a string, however, whenever I compile it, error occurs, it says: "warning: address ...

  1. #1
    Yin
    Guest

    Question How to write a function that returns a string?

    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?

  2. #2
    Yin
    Guest

    Question Example:

    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.

  3. #3
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    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

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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.
    Code:
    #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; 
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113

    Re: Example:

    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 01: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

  6. #6
    Yin
    Guest

    Question

    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?

  7. #7
    Yin
    Guest
    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.
    Code:
    #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; 
    }
    -Prelude
    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.

  8. #8
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    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 01: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

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I whipped up an example similar to Corbras2, and it worked also.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. Custom String Parsing Function
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-01-2002, 11:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21