Thread: Return a string

  1. #16
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by quzah
    You can return an array that way, provided it is static. Ever hear of strtok?
    Code:
    char *foo( ... )
    {
        static char buf[ BUFSIZ ];
        ...
        return buf;
    }

    Quzah
    Isthere any advantage in this practice comparedto my solution?

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes, quzah's solution will actually return the array. Your suggestion will not do that.

  3. #18
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Tonto
    C strings are null-terminated, and you can count how many characters there are before the null byte. Making all your variables global is really wierd to say the least.
    But the compiled code doesn't know it is a string when returning
    variables,, it just knows the lenght of a variable and pushs
    that much data on to the stack so the data must be of a fixed
    not variable lenght.

    I see loads of poor programmers passing data about which should obviously be global.

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    When returning a string you are returning a pointer. Return values aren't placed on the stack they are returned in CPU registers. On x86 systems that's either a 32 bit value (EAX) or 64 bit value (EDX:EAX).

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > Isthere any advantage in this practice comparedto my solution?
    static arrays have the problem of what to do if the answer doesn't fit.
    allocated memory has the problem of telling the caller that the memory needs to be freed.

    Make the caller pass a pointer to where the answer is to be stored, and it's length.
    This makes the function really easy to write. The caller is free to choose any kind of array or allocated memory approach it wants to, and the service function just does it's stuff.

    > I see loads of poor programmers passing data about which should obviously be global.
    Boy am I glad I don't have to maintain any of your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #21
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Quantum1024
    When returning a string you are returning a pointer. Return values aren't placed on the stack they are returned in CPU registers. On x86 systems that's either a 32 bit value (EAX) or 64 bit value (EDX:EAX).
    However the OP was concerned with returning as string,
    not a string pointer. If you had a string 2000 bytes long I think
    you would be scratchinig around for spare registers!!

  7. #22
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    esbo can you please give an example of a c function that returns a "string" and not a char *

  8. #23
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by spydoor
    esbo can you please give an example of a c function that returns a "string" and not a char *

    Well as I am explaining why it can't be done that puts me
    in a rather tricky situation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM