Thread: Returning a string from a function

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    Returning a string from a function

    Yes - I'm new to this!

    I'm trying to make a function return a string to the another section of my program.

    The function itself is called headercreate and currently looks like this:

    Code:
    char headercreate(char pagetitle)[1000]
     {
    	 /*Creates a XHTML 1.1 Compatible Header*/
    	char tmptxt[1000];
    strcpy(tmptxt, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
    return tmptxt;
    
    
     }
    this doesn't work, as I've found out, because I can't return a string. I believe I have to used pointers, but reading the pointer chapter of O'Reilly's Practical C++ Programming has left me none the wiser. Any help would be great!

    Thanks, Tom

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    most functions that return strings will have the buffer passed in:

    char str[256];

    GetString(str, 256);

    This allows the memory to exist outside the function. Some alternatives are passing out a pointer to a global buffer (eww) or passing out memory that was allocated inside the function (ewwwww).

    but here's my function:

    Code:
    void GetString(char *str, int size)
    {
         //do some stuff to fill int he string
    }
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    or you may use std::string
    Code:
    #include <string>
    
    using namespace std;
    
    void GetString(string& retStr) {
        retStr = "something here";
    }
    I prefer to use std::string over char*, it's always cleaner, and with some reference passing, you can avoid performance issues.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    66
    There is also a C++ version that works:

    Code:
    #include <iostream>
    
    using namespace std;
    
    string getMyName()
    {
      string strRet;  
      
      // Something complicated here
    
      string = "foo";
    
      return strRet;
    }
    
    int main(it argc, char **argv)
    {
      string myName = getMyName();
      cout << "My name is: " << myName.c_str() << endl;
    
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    Smile

    Wow! You are all so helpful - thanks so much, and I shall try to put this into use tomorrow, but again, thanks for putting in the effort to help me!

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    In achacha's code, you need to include <string> as in glUser3f's code.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    5
    I'm confused by achacha's code. What is strRet , it doesn't appear to ever get anything assigned to it. (If it does, then why does string="foo"; appear?

    In main, what are it argc, char **argv and why do I have to put myName.c_str() rather than just myName ?

    thanks very much again

    Tom

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    its just a typo. achacha meant to use the same variable. People usually type code right into the 'reply' window without trying it in a compiler first.

    The only reason that you might not use the "C++ version" is that it causes copies of the string to be made. A temporary std::string is created to return the value. The string is copied from the local one to the outside one and the allocation is done for each. So if speed is a concern, this might not be the way to go.

    It just so happens that the C way is also the C++ way because C++ includes that ability. The thing that makes C++ better than Java is that it is a hybrid of low level control and high level OOP. So you have all the control of C with all of the beauty of classes.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #9
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361

    lol

    Maybe I didn't read enough, but if anyone hasn't noticed...

    ORIGINAL CODE:
    Code:
    char headercreate(char pagetitle)[1000]
     {
    	 /*Creates a XHTML 1.1 Compatible Header*/
    	char tmptxt[1000];
    strcpy(tmptxt, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
    return tmptxt;
     }
    Unless I'm making an idiot of myself (I usually am) could he not do this instead?

    Code:
    char *headercreate()//no need for pagetitle so far
    {
    	 /*Creates a XHTML 1.1 Compatible Header*/
    	char tmptxt[1000];
    strcpy(tmptxt, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
    return tmptxt;
     }
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  10. #10
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448

    Re: lol

    Originally posted by Stan100
    Unless I'm making an idiot of myself (I usually am) could he not do this instead?
    Code:
    char *headercreate()//no need for pagetitle so far
    {
    	 /*Creates a XHTML 1.1 Compatible Header*/
    	char tmptxt[1000];
    strcpy(tmptxt, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
    return tmptxt;
     }
    Actually, when the function returns, the memory tmptxt is poiting to is de-allocated, so any program can make use of it. If you did somthing like strcpy(newstr, headercreate()) it might work, since the memory may not have been re-allocated, but you shoudn't rely on that.
    If you just kept the pointer, you could be pointing to another program's memory space, your OS would give you an error, and close the app (or debbug it if you have that option).
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    66
    Sorry about that, my compiler was tied up and I thought I could get away with it... I was wrong

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string getMyName()
    {
      string strRet;
    
      // Something complicated here
    
      strRet = "foo";
    
      return strRet;
    }
    
    int main(int argc, char **argv)
    {
      string myName = getMyName();
      cout << "My name is: " << myName.c_str() << endl;
    
      return 0;
    }

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. Returning a string from function
    By badmantel in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 12:17 PM
  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