Returning char** from a function.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • noridotjabi@gmail.com

    Returning char** from a function.

    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?

  • Ben Pfaff

    #2
    Re: Returning char** from a function.

    "noridotjabi@gm ail.com" <noridotjabi@gm ail.comwrites:
    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

    Comment

    • Ike Naar

      #3
      Re: Returning char** from a function.

      In article <1154369713.630 679.18790@s13g2 000cwa.googlegr oups.com>,
      noridotjabi@gma il.com <noridotjabi@gm ail.comwrote:
      >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.hfor 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

      Comment

      • Andrey Tarasevich

        #4
        Re: Returning char** from a function.

        oridotjabi@gmai l.com 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

        Comment

        • Eric Sosman

          #5
          Re: Returning char** from a function.

          noridotjabi@gma il.com 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
          esosman@acm-dot-org.invalid

          Comment

          • Chris Dollin

            #6
            Re: Returning char** from a function.

            noridotjabi@gma il.com 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, incomprehensibl y 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/

            Comment

            Working...