Thread: Returning a string from function

  1. #1
    Registered User
    Join Date
    Oct 2003

    Returning a string from function

    How do I return an string from a function.

    void main {
         Char str[];
       getstr() {
           string="I work";

  2. #2
    Join Date
    Sep 2003
    You can't return arrays, only pointers to char and pointers to arrays. So your code would look like this:
    char *getstr() {
            return "I work";
    int main {
            char *str;
            str = getstr():
            printf("%s", str);
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Phildelphia, PA
    You could return an STL string. Those are much easier to work with anyway. You can do that like any other function:
    string my_func();

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Registered User
    Join Date
    Nov 2002
    oh, and don't use void main.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. returning a string from a function
    By itld in forum Linux Programming
    Replies: 5
    Last Post: 12-03-2001, 01:35 AM