Make function return string...

This is a discussion on Make function return string... within the C Programming forums, part of the General Programming Boards category; Simple question: How can i make a function return a string? I have tried this: Code: char "name of function"(Arguments) ...

  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 07: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 09:36 AM.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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
    22,274
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 11:51 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,274
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,274
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 07:54 PM
  2. Replies: 6
    Last Post: 04-21-2006, 09: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

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