Returning char** from a function.

Discussion in 'C Programming' started by noridotjabi@gmail.com, Jul 31, 2006.

  1. Guest

    Why is it not possible to return char** from a funcion. For example:

    char** foo(void) {
    char[][] foobar = {"foo", "bar"};

    return foobar;
    }

    int main(void) {
    printf("%s\n", foo()[0]);

    return 0;
    }

    I have no idea why this does not work. Maybe I am doing something
    wrong? This is not the actual code that I am comping but I didn't want
    to post that as the relevant code is 200 lines long. I get warnings
    when I try to compile this and a segmentation fault upon function call.
    Thanks.
    Nori

    P.S. Is there anyway to get rid of that pesky "function returns adress
    of local variable" warning?
    , Jul 31, 2006
    #1
    1. Advertising

  2. Ben Pfaff Guest

    "" <> writes:

    > Why is it not possible to return char** from a funcion. For example:
    >
    > char** foo(void) {
    > char[][] foobar = {"foo", "bar"};
    >
    > return foobar;
    > }


    There are at least three problems here. First, char[][] is not a
    valid type. Only the first in a sequence of array declarators
    may have empty brackets.

    Second, if it were a valid type, char[][] would not be the same
    type as char **. The name of an array used in an expression is,
    in most circumstances, converted to a pointer to its first
    element, which in this case would result in type "char (*)[]",
    that is, a pointer to an array. A pointer to an array is not
    compatible with a pointer to a pointer.

    > P.S. Is there anyway to get rid of that pesky "function returns adress
    > of local variable" warning?


    Third, you're trying to return the address of a local variable.
    The correct thing to do is to not return the address of a local
    variable. One way to do that would be to declare the array
    "static", but I'd recommend doing that only if its value never
    changes, in which case "const" might also be warranted.
    Alternatively, you could dynamically allocate your array.
    --
    "For those who want to translate C to Pascal, it may be that a lobotomy
    serves your needs better." --M. Ambuhl

    "Here are the steps to create a C-to-Turbo-Pascal translator..." --H. Schildt
    Ben Pfaff, Jul 31, 2006
    #2
    1. Advertising

  3. Ike Naar Guest

    In article <>,
    <> wrote:
    >Why is it not possible to return char** from a funcion. For example:
    >
    >char** foo(void) {
    > char[][] foobar = {"foo", "bar"};


    This is a syntax error, the '[]' should follow 'foobar'.

    > return foobar;


    You are returning a pointer to a local variable;
    foobar ceases to exist when you return from the function.

    A possible solution is to make foobar static:
    static char * foobar[] = {"foo", "bar"};

    >}
    >
    >int main(void) {
    > printf("%s\n", foo()[0]);


    You also need to #include <stdio.h> for the prototype of printf()

    > return 0;
    >}
    >
    >I have no idea why this does not work. Maybe I am doing something
    >wrong? This is not the actual code that I am comping but I didn't want
    >to post that as the relevant code is 200 lines long. I get warnings
    >when I try to compile this and a segmentation fault upon function call.


    >P.S. Is there anyway to get rid of that pesky "function returns adress
    >of local variable" warning?


    Yes. Don't return the address of a local variable.

    Regards,
    Ike
    Ike Naar, Jul 31, 2006
    #3
  4. wrote:
    > Why is it not possible to return char** from a funcion.


    It _is _ possible to return a 'char**' from a function.

    > For example:
    >
    > char** foo(void) {
    > char[][] foobar = {"foo", "bar"};


    Invalid declaration.

    Firstly, in an array declaration with an initializer only the very first pair of
    square brackets can be left empty. The one that follow must specify a concrete size.

    Secondly, the '[]' is supposed to follow the identifier, not the type name

    char foobar[][4] = {"foo", "bar"};

    > return foobar;


    'foobar' is a two-dimensional array of 'char', not a 'char**'. It cannot be
    returned as 'char**'.

    > P.S. Is there anyway to get rid of that pesky "function returns adress
    > of local variable" warning?


    Yes. Stop doing this. Returning address of local object (an that's what you are
    trying to do) is completely useless.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Jul 31, 2006
    #4
  5. Eric Sosman Guest

    wrote:
    > [...]
    > P.S. Is there anyway to get rid of that pesky "function returns adress
    > of local variable" warning?


    Easy: Don't return pointers to local variables, which will
    cease to exist before the caller receives the returned pointer.

    "Go to the stadium, wearing a yellow carnation in your
    right lapel and carrying a copy of `Winnie the Pooh' in your
    left hand, and deliver this envelope to the occupant of seat
    49B in section 22."

    "Sure, boss -- any particular game you had in mind?"

    --
    Eric Sosman
    lid
    Eric Sosman, Aug 1, 2006
    #5
  6. Chris Dollin Guest

    wrote:

    > P.S. Is there anyway to get rid of that pesky "function returns adress
    > of local variable" warning?


    Yes. Don't return the address of a local variable.

    (The variable evaporates when you leave the function, so the address
    becomes meaningless, and any use of it -- /any/ use of it -- permits
    the implementation to do whatever it likes. If you're lucky, you'll
    get an immediate loud program failure. If not, you're code will break
    later, incomprehensibly and possibly expensively.)

    --
    Chris "where does a flame go when it's blown out?" Dollin
    "I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/
    Chris Dollin, Aug 1, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    542
  2. wwj
    Replies:
    24
    Views:
    2,484
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    463
    Tristan Miller
    Jan 17, 2004
  4. Replies:
    8
    Views:
    527
    Bryan Donlan
    Aug 26, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,015
    Ian Collins
    May 9, 2006
Loading...

Share This Page