Thread: return array of pointers in function prototype

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242

    return array of pointers in function prototype

    Hi all.

    If I have a function named getwords(void), and I want the function to return an array of pointers to type char, how do I declare the return type in the function prototype?

    (char *[])getwords(void)?

    Thanks in advance.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would use a typedef, but note that you cannot actually have an array type as the return type, so it would be a pointer type instead. Actually, why not have the caller pass a pointer as an argument?
    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

  3. #3
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    I would like to return an array of pointers back to main(). So what should the return type look like? When you say a pointer type, do you mean like below? Thanks.

    Code:
    #include <stdio.h>
    
    char *test(void);
    
    int main(void)
    {
    
        int i;
        char *array[4];
    
        array = test();
    
        for(i = 0; i < 4; i++)
        {
            printf("%s", array[i]);
        }
    
        return 0;
    }
    
    char *test(void)
    {
        char *string[4] = {"How", "now", "brown", "cow"};
    
        return string;
    }
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That array is local to the test function, with automatic storage duration. Returning a pointer to the first element of that array will likely be a mistake still accessing that array using that pointer in main would result in undefined behaviour.

    One way out could be to declare the variable as static:
    Code:
    #include <stdio.h>
    
    const char * const *test(void);
    
    int main(void)
    {
    
        int i;
        const char * const *array;
    
        array = test();
    
        for(i = 0; i < 4; i++)
        {
            printf("%s", array[i]);
        }
    
        return 0;
    }
    
    const char * const *test(void)
    {
        static const char * const string[4] = {"How", "now", "brown", "cow"};
    
        return string;
    }
    Note that I also introduced const qualification as is proper to the string literals and the presumption that the static array should not be changed at all.
    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

  5. #5
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Quote Originally Posted by laserlight View Post

    const char * const *test(void);
    I've never seen two * and two const in a function prototype before.

    Thanks for the replies.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by cfanatic View Post
    I've never seen two * and two const in a function prototype before.
    I think here it is a good link const-correctness - Wikipedia, the free encyclopedia

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by cfanatic View Post
    I've never seen two * and two const in a function prototype before.
    I think here it is a good link const-correctness - Wikipedia, the free encyclopedia

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I learned so much from this site years ago:

    http://untroubled.org/articles/cdecls.txt

    Building on laserlights example and the tutorial I have given above, here is another way of doing it:

    "Declare test as a function returning a pointer to an array of constant char pointers"
    const char *(*test())[];
    Code:
    #include <stdio.h>
    
    
    const char *(*test())[];
    
    
    int main(void)
    {
    
    
        int i;
        const char * const *array;
    
    
        array = test();
    
    
        for(i = 0; i < 4; i++)
        {
            printf("%s", array[i]);
        }
    
    
        return 0;
    }
    
    
    const char *(*test())[]
    {
        static const char * const string[4] = {"How", "now", "brown", "cow"};
    
    
        return string;
    }
    Fact - Beethoven wrote his first symphony in C

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    anonymous function? (did they even exsist in C? )

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    anonymous function? (did they even exsist in C? )
    Now, you are going to have to help me with that - I had a look around and saw a definition of "anonymous function" being one declared without a name (I'm sure that it is much more than that). Also, I confirmed that they definitely don't exist in C (But have recently been added to C++) - Anonymous function - Wikipedia, the free encyclopedia

    @std10093: Would you be able to help me with what you meant by that post?

    I should correct my code from my last post - You will need to add 'void' to the inputs of the function.

    i.e.
    Code:
    const char *(*test(void))[];
    Fact - Beethoven wrote his first symphony in C

  11. #11
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Click_here View Post
    Now, you are going to have to help me with that - I had a look around and saw a definition of "anonymous function" being one declared without a name (I'm sure that it is much more than that). Also, I confirmed that they definitely don't exist in C (But have recently been added to C++) - Anonymous function - Wikipedia, the free encyclopedia

    @std10093: Would you be able to help me with what you meant by that post?

    I should correct my code from my last post - You will need to add 'void' to the inputs of the function.

    i.e.
    Code:
    const char *(*test(void))[];
    You also need to remove the brackets at the end. They're not needed (and it causes the test function to return a different pointer type than the value it's returning and different type than the array variable).

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Click_here View Post
    @std10093: Would you be able to help me with what you meant by that post?
    Excuse me,but the word anonymous is greek so the meaning comes to my immediately , that's why i did not give a definition of the world
    Anonymous function means a function with no name.I was taken away by web development,that's why it came to my mind
    And at last ,it is not an anonymous function

  13. #13
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You also need to remove the brackets at the end. They're not needed (and it causes the test function to return a different pointer type than the value it's returning and different type than the array variable).



    I couldn't agree more - I think that it's important to have your declaration match what you meant. In the other post, it would have been better to use the same as the declaration as the variable (looking back at it, I think that it was a fluke that it worked)


    On my version of codeblocks, removing the square brackets will also work in this (quite different, but more appropriate) example, but I think that it is more descriptive of what is going on to leave the square brackets in -


    Code:
    
    #include <stdio.h>
    
    
    typedef enum
    {
        brown_cow=0,
        black_cow,
        white_cow
    
    
    }   cow_colour;
    
    
    /******************************
     * declare "test" as a function
     * with input "cow_colour"
     * returning a pointer to
     * an array with 4 elements of
     * constant char pointers
     ******************************/
    const char * (* test(cow_colour))[4];
    
    
    int main(void)
    {
        int i;
    
    
        const char * (* array)[4] = test(brown_cow);
        //const char * (* array)[4] = test(black_cow);
        //const char * (* array)[4] = test(white_cow);
    
    
        for(i = 0; i < 4; i++)
        {
            printf("%s ", (*array)[i]);
        }
    
    
        return 0;
    }
    
    
    const char * (* test(cow_colour cow))[4]
    {
        static const char *brown_str[4] = {"How", "now", "brown", "cow"};
        static const char *black_str[4] = {"How", "now", "black", "cow"};
        static const char *white_str[4] = {"How", "now", "white", "cow"};
        static const char *unknown_str[4] = {"How", "now", "unknown coloured", "cow"};
    
    
        switch (cow)
        {
            case brown_cow:
                return &brown_str;
    
    
            case black_cow:
                return &black_str;
    
    
            case white_cow:
                return &white_str;
    
    
        }
    
    
        return &unknown_str;
    }

    If anyone has trouble reading the declaration, they should look at the tutorial that I put up before - http://untroubled.org/articles/cdecls.txt
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 05-30-2011, 08:26 AM
  2. Replies: 3
    Last Post: 07-15-2010, 09:14 AM
  3. function prototype with array
    By melodia in forum C Programming
    Replies: 3
    Last Post: 11-15-2009, 02:31 PM
  4. Replies: 2
    Last Post: 06-27-2009, 06:57 AM
  5. Replies: 2
    Last Post: 11-27-2007, 01:01 AM