Thread: Make function return string...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Location
    Denmark
    Posts
    22

    Make function return string...

    Simple question: How can i make a function return a string?

    I have tried this:

    Code:
    char "name of function"(Arguments) {
    char string1[16];
    string1 = "Hello World";
    return string1;
    }
    Thanks in advance!

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes, except the string will be destroyed when the function returns. Not to mention the return type 'char' won't work for arrays.

    What you want is to return an address to a static variable in the function (note that means you'll only be able to return one string at any one time).

    Code:
    char * nameOfFunction(void)
    {
        static char str[] = "Hello world";
        return str;
    }
    Or a better technique,
    Code:
    void nameOfFunction(char * buff, size_t n)
    {
        if(buff == NULL)
            return;
        memset(buff, 0, n);
        strncpy(buff, "Hello world", n - 1);
        return;
    }
    
    /* ... */
    
    char str[256];
    nameOfFunction(str, sizeof(str));
    printf("%s\n", str);
    Last edited by zacs7; 07-05-2008 at 06:47 AM.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Location
    Denmark
    Posts
    22
    What is a static variabel? and whats is void?
    Last edited by MKirstensen; 07-05-2008 at 08:36 AM.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I think you need a book, tutorial, reference or something at this point.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Location
    Denmark
    Posts
    22
    Quote Originally Posted by zacs7 View Post
    Yes, except the string will be destroyed when the function returns. Not to mention the return type 'char' won't work for arrays.

    What you want is to return an address to a static variable in the function (note that means you'll only be able to return one string at any one time).

    Code:
    char * nameOfFunction(void)
    {
        static char str[] = "Hello world";
        return str;
    }
    Or a better technique,
    Code:
    void nameOfFunction(char * buff, size_t n)
    {
        if(buff == NULL)
            return;
        memset(buff, 0, n);
        strncpy(buff, "Hello world", n - 1);
        return;
    }
    
    /* ... */
    
    char str[256];
    nameOfFunction(str, sizeof(str));
    printf("%s\n", str);
    There is something i dont get

    When i call the function:

    Code:
    nameOfFunction(str, sizeof(str));
    Why isnt there & in front of str, cause it seems logical to me that you passes the address into the pointer buff, and not the data inside str.

    And why do you need to fill buff up with 0?

    And finally why isnt there a * in front of buff in:
    Code:
        memset(buff, 0, n);
        strncpy(buff, "Hello world", n - 1);
    Thanks in advance!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why isnt there & in front of str, cause it seems logical to me that you passes the address into the pointer buff, and not the data inside str.
    When an array is passed as an argument, it is converted to a pointer to its first element, thus the address of its first element is passed.

    And why do you need to fill buff up with 0?
    To ensure that there is a null character at the end of the string.

    And finally why isnt there a * in front of buff in:
    buff is the string, *buff is the first character of the string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2008
    Location
    Denmark
    Posts
    22
    Thanks!

  8. #8
    Registered User
    Join Date
    Jul 2008
    Location
    Denmark
    Posts
    22
    Quote Originally Posted by laserlight View Post
    buff is the string, *buff is the first character of the string.
    So you mean that when i use buff in memset function for example, the compiler already looks at it as a pointer to the mem block, but when u uses the * it just points to the first section in the array?
    Last edited by MKirstensen; 07-06-2008 at 10:51 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So inside buff we have the memory address of str...right?
    No, buff is a pointer that (initially) contains the address of the first character of str.

    And dont i have to tell the compiler that buff is a pointer and it shut write to the memory address inside? ... and why not?
    By declaring buff as a parameter with a type of char*, you have told the compiler that buff is a pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jul 2008
    Location
    Denmark
    Posts
    22
    Thanks sorry for the edit...

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So you mean that when i use buff in memset function for example, the compiler already looks at it as a pointer to the mem block, but when u uses the * it just points to the first section in the array?
    When I take one glance at this:
    Code:
    void nameOfFunction(char * buff, size_t n)
    I can tell you that buff is a pointer to a char. Since buff is a pointer to a char, *buff is a char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Replies: 6
    Last Post: 04-21-2006, 08:49 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM