Passing pointer array to function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tatu Portin

    #1

    Passing pointer array to function

    I have a struct:

    typedef struct {
    char **user_comments ;
    int *comment_wds;
    int comments;
    char *vendor;
    } vorbis_comment;


    void func (char **table)
    {
    return;
    }

    int main (void)
    {
    vorbis_comment vc;


    /* This part GCC doesn't like */

    func (vc.user_commen ts);



    return 0;
    }

    GCC (3.1) would say that[color=blue]
    >warning: passing arg 1 of 'func' from incompatible pointer type[/color]

    But still my code in 'func' (not posted, above is an example) works correctly,
    utilizing whole '**table'.

    Now what I want to ask is that which is the legal way to make a call of this
    kind? Or is it just a bug or "feature" of GCC?


    I am compiling with a commandline:

    gcc -Wall -pedantic -ansi -mcpu=athlon -ffast-math -O2
  • Mike Wahler

    #2
    Re: Passing pointer array to function


    "Tatu Portin" <axel86@mbnet.f i> wrote in message
    news:TENud.449$ 186.312@read3.i net.fi...[color=blue]
    > I have a struct:
    >
    > typedef struct {
    > char **user_comments ;
    > int *comment_wds;
    > int comments;
    > char *vendor;
    > } vorbis_comment;
    >
    >
    > void func (char **table)
    > {
    > return;
    > }
    >
    > int main (void)
    > {
    > vorbis_comment vc;
    >
    >
    > /* This part GCC doesn't like */
    >
    > func (vc.user_commen ts);
    >
    >
    >
    > return 0;
    > }
    >
    > GCC (3.1) would say that[color=green]
    > >warning: passing arg 1 of 'func' from incompatible pointer type[/color]
    >
    > But still my code in 'func' (not posted,[/color]

    Why not?
    [color=blue]
    > above is an example) works correctly,
    > utilizing whole '**table'.
    >
    > Now what I want to ask is that which is the legal way to make a call of[/color]
    this[color=blue]
    > kind? Or is it just a bug or "feature" of GCC?[/color]

    We cannot tell you the correct form for calling a function whose
    definition (at least declaration) that we cannot see. What is
    the signature of 'func()'? Remember that an array is not a pointer,
    and a pointer is not an array.

    -Mike


    Comment

    • E. Robert Tisdale

      #3
      Re: Passing pointer array to function

      Tatu Portin wrote:
      [color=blue]
      > I have a struct:
      >
      > cat main.c[/color]
      typedef struct {
      char** user_comments;
      int* comment_wds;
      int comments;
      char* vendor;
      } vorbis_comment;


      void func(char** table) {
      return;
      }

      int main(int argc, char* argv[]) {
      vorbis_comment vc;

      /* GCC doesn't like this part. */

      func(vc.user_co mments);

      return 0;
      }
      [color=blue]
      > gcc -Wall -ansi -pedantic -ffast-math -O2 -o main main.c
      > gcc --version[/color]
      gcc (GCC) 3.4.1
      [color=blue]
      >
      > GCC (3.1) would say that[color=green]
      > >warning: passing arg 1 of 'func' from incompatible pointer type[/color][/color]

      It seems to compile just fine for me.

      Comment

      • Joe Wright

        #4
        Re: Passing pointer array to function

        Tatu Portin wrote:[color=blue]
        > I have a struct:
        >
        > typedef struct {
        > char **user_comments ;
        > int *comment_wds;
        > int comments;
        > char *vendor;
        > } vorbis_comment;
        >
        >
        > void func (char **table)
        > {
        > return;
        > }
        >
        > int main (void)
        > {
        > vorbis_comment vc;
        >
        >
        > /* This part GCC doesn't like */
        >
        > func (vc.user_commen ts);
        >
        >
        >
        > return 0;
        > }
        >
        > GCC (3.1) would say that[color=green]
        > >warning: passing arg 1 of 'func' from incompatible pointer type[/color]
        >
        > But still my code in 'func' (not posted, above is an example) works
        > correctly, utilizing whole '**table'.
        >
        > Now what I want to ask is that which is the legal way to make a call of
        > this kind? Or is it just a bug or "feature" of GCC?
        >
        >
        > I am compiling with a commandline:
        >
        > gcc -Wall -pedantic -ansi -mcpu=athlon -ffast-math -O2[/color]

        Your code exactly as written here, pasted to tatu.c and compiled with ..

        gcc -Wall -pedantic -ansi -mcpu=athlon -ffast-math -O2 tatu.c

        ... compiles here without error.

        C:\work\c\clc>g cc --version
        gcc.exe (GCC) 3.1
        Copyright (C) 2002 Free Software Foundation, Inc.

        --
        Joe Wright mailto:joewwrig ht@comcast.net
        "Everything should be made as simple as possible, but not simpler."
        --- Albert Einstein ---

        Comment

        • Tatu Portin

          #5
          Re: Passing pointer array to function

          Ok. First post was wrong. (I thought that simplifying would do the same. It
          didn't.)

          Here we are:


          42:int fprint_comments _formatted
          43: ( FILE *tg
          44: , const vorbis_comment *vc)
          45:{
          46: register int i;

          ....

          69: if (flag) {

          70: entry = has_str (s_album, vc->user_comment s, vc->comments);

          71: val = strpbrk (vc->user_comment s[entry], "=");
          72: val++;
          73: fprintf (tg, "%s\n", val);
          74:
          75: flag = 0;
          76: }

          ....

          90: return 0;
          91:}

          gcc -Wall -pedantic -ansi -mcpu=athlon -ffast-math -O2 cdmaker.c cdmaker_1.o -o
          cdmaker.exe
          cdmaker.c In function 'fprint_comment s_formatted':
          cdmaker.c:70 warning: passing arg 2 of 'has_str' from incompatible pointer type


          Function prototypes:


          int fprint_comments _formatted
          ( FILE *tg
          , const vorbis_comment *vc);

          int has_str
          ( const char *str
          , const char **table
          , int ent); /* Number of entries in '**table' */


          Struct definition:


          typedef struct {
          char **user_comments ;
          int *comment_wds;
          int comments;
          char *vendor;
          } vorbis_comment;

          Comment

          • Raymond Martineau

            #6
            Re: Passing pointer array to function

            On Sun, 12 Dec 2004 03:43:16 GMT, Tatu Portin <axel86@mbnet.f i> wrote:
            [color=blue]
            >Ok. First post was wrong. (I thought that simplifying would do the same. It
            >didn't.)
            >
            >Here we are:
            >
            >
            >42:int fprint_comments _formatted
            >43: ( FILE *tg
            >44: , const vorbis_comment *vc)
            >45:{
            >46: register int i;
            >
            >...
            >
            >69: if (flag) {
            >
            >70: entry = has_str (s_album, vc->user_comment s, vc->comments);[/color]

            You might get a bit more help compiling it with a C++ compiler:

            E:\temp>gpp -Wall a.cpp
            a.cpp: In function `int main()':
            a.cpp:27: error: invalid conversion from `char**' to `const char**'
            a.cpp:27: error: initializing argument 2 of `int has_str(const char*,
            const char**, int)'

            It's valid in C, but GCC decides it's worth raising a warning about
            converting 'char**' to 'const char**'. You can get rid of the warning by
            doing a type cast, but there may be better methods of dealing with it.

            Comment

            • Chris Torek

              #7
              Re: Passing pointer array to function

              In article <o7Pud.3$mk7.1@ read3.inet.fi> Tatu Portin <axel86@mbnet.f i> wrote:[color=blue]
              >Ok. First post was wrong. (I thought that simplifying would do the same. It
              >didn't.)[/color]

              Always test the simplified-for-post version. :-)

              Without quoting a lot, your problem is the "const" qualifier,
              which does not work right in C. Stop using it, and the problem
              will go away (this is not necessarily the best solution, but is
              certainly the easiest to describe).

              See also the comp.lang.c FAQ, question 11.10.
              --
              In-Real-Life: Chris Torek, Wind River Systems
              Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
              email: forget about it http://web.torek.net/torek/index.html
              Reading email is like searching for food in the garbage, thanks to spammers.

              Comment

              • CBFalconer

                #8
                Re: Passing pointer array to function

                Mike Wahler wrote:[color=blue]
                >
                > "Tatu Portin" <axel86@mbnet.f i> wrote in message
                > news:TENud.449$ 186.312@read3.i net.fi...[color=green]
                > > I have a struct:
                > >
                > > typedef struct {
                > > char **user_comments ;
                > > int *comment_wds;
                > > int comments;
                > > char *vendor;
                > > } vorbis_comment;
                > >
                > >
                > > void func (char **table)
                > > {
                > > return;
                > > }
                > >
                > > int main (void)
                > > {
                > > vorbis_comment vc;
                > >
                > >
                > > /* This part GCC doesn't like */
                > >
                > > func (vc.user_commen ts);
                > >
                > >
                > >
                > > return 0;
                > > }
                > >
                > > GCC (3.1) would say that[color=darkred]
                > > >warning: passing arg 1 of 'func' from incompatible pointer type[/color]
                > >
                > > But still my code in 'func' (not posted,[/color]
                >
                > Why not?
                >[color=green]
                > > above is an example) works correctly,
                > > utilizing whole '**table'.
                > >
                > > Now what I want to ask is that which is the legal way to make a call of[/color]
                > this[color=green]
                > > kind? Or is it just a bug or "feature" of GCC?[/color]
                >
                > We cannot tell you the correct form for calling a function whose
                > definition (at least declaration) that we cannot see. What is
                > the signature of 'func()'? Remember that an array is not a pointer,
                > and a pointer is not an array.[/color]

                Er - he gave the full definition of func. What he didn't do was
                initialize vc, which should not cause a compile error.

                --
                Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                Available for consulting/temporary embedded and systems.
                <http://cbfalconer.home .att.net> USE worldnet address!


                Comment

                • Mike Wahler

                  #9
                  Re: Passing pointer array to function

                  "CBFalconer " <cbfalconer@yah oo.com> wrote in message
                  news:41BBDB48.E 5F49114@yahoo.c om...[color=blue]
                  > Mike Wahler wrote:[color=green]
                  > >
                  > > We cannot tell you the correct form for calling a function whose
                  > > definition (at least declaration) that we cannot see. What is
                  > > the signature of 'func()'? Remember that an array is not a pointer,
                  > > and a pointer is not an array.[/color]
                  >
                  > Er - he gave the full definition of func. What he didn't do was
                  > initialize vc, which should not cause a compile error.[/color]

                  Um, yes, it seems that it's me who can't see today. :-)

                  -Mike


                  Comment

                  • Chris Torek

                    #10
                    Re: Passing pointer array to function

                    In article <8emnr01ei6qrt7 l0a8ekqm0hehjnk 9c50j@4ax.com>
                    Raymond Martineau <bk039@ncf.ca > wrote:[color=blue]
                    >You might get a bit more help compiling it with a C++ compiler:
                    >
                    >E:\temp>gpp -Wall a.cpp
                    >a.cpp: In function `int main()':
                    >a.cpp:27: error: invalid conversion from `char**' to `const char**'
                    >a.cpp:27: error: initializing argument 2 of `int has_str(const char*,
                    >const char**, int)'
                    >
                    >It's valid in C ...[/color]

                    Actually, it is *not* valid in C either.
                    [color=blue]
                    >but GCC decides it's worth raising a warning about converting
                    >'char**' to 'const char**'.[/color]

                    GCC simply chooses to complain-and-keep-going in this case, rather
                    than complain-and-stop-compiling. GCC calls the former a "warning"
                    and the latter an "error", but the C standard says only that the
                    conversion is incorrect and must elicit a "diagnostic ".

                    The set of things-that-cause-stopping is different for GCC's C
                    compiler than for GCC's C++ compiler, partly because they are
                    maintained by different people, partly because C compilers have
                    traditionally accepted all kinds of invalid source and generated
                    machine code anyway (which promptly core-dumps, in many cases),
                    and of course partly because the languages are different (although
                    in this case, the semantics actually match up, for once).
                    [color=blue]
                    >You can get rid of the warning by doing a type cast, but there may
                    >be better methods of dealing with it.[/color]

                    In C++, if you made has_str() take a "const char *const *" parameter,
                    the problem would go away, but it would remain a problem in C.
                    --
                    In-Real-Life: Chris Torek, Wind River Systems
                    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                    email: forget about it http://web.torek.net/torek/index.html
                    Reading email is like searching for food in the garbage, thanks to spammers.

                    Comment

                    • Tatu Portin

                      #11
                      Re: Passing pointer array to function

                      Tatu Portin wrote:[color=blue]
                      > Ok. First post was wrong. (I thought that simplifying would do the same.
                      > It didn't.)
                      >
                      > Here we are:
                      >
                      >
                      > 42:int fprint_comments _formatted
                      > 43: ( FILE *tg
                      > 44: , const vorbis_comment *vc)
                      > 45:{
                      > 46: register int i;
                      >
                      > ....
                      >
                      > 69: if (flag) {
                      >
                      > 70: entry = has_str (s_album, vc->user_comment s, vc->comments);
                      >
                      > 71: val = strpbrk (vc->user_comment s[entry], "=");
                      > 72: val++;
                      > 73: fprintf (tg, "%s\n", val);
                      > 74:
                      > 75: flag = 0;
                      > 76: }
                      >
                      > ....
                      >
                      > 90: return 0;
                      > 91:}
                      >
                      > gcc -Wall -pedantic -ansi -mcpu=athlon -ffast-math -O2 cdmaker.c
                      > cdmaker_1.o -o cdmaker.exe
                      > cdmaker.c In function 'fprint_comment s_formatted':
                      > cdmaker.c:70 warning: passing arg 2 of 'has_str' from incompatible
                      > pointer type
                      >
                      >
                      > Function prototypes:
                      >
                      >
                      > int fprint_comments _formatted
                      > ( FILE *tg
                      > , const vorbis_comment *vc);
                      >
                      > int has_str
                      > ( const char *str
                      > , const char **table
                      > , int ent); /* Number of entries in '**table' */
                      >
                      >
                      > Struct definition:
                      >
                      >
                      > typedef struct {
                      > char **user_comments ;
                      > int *comment_wds;
                      > int comments;
                      > char *vendor;
                      > } vorbis_comment;[/color]


                      Thank you all. The problem was, as you said, the const qualifier.

                      But is anyone capable of saying why exactly this fails? (with 'const char **table')

                      Comment

                      • Andrey Tarasevich

                        #12
                        Re: Passing pointer array to function

                        Tatu Portin wrote:[color=blue]
                        > ...
                        > But is anyone capable of saying why exactly this fails? (with 'const char **table')
                        > ...[/color]

                        The reason why type 'T**' is not implicitly convertible to type 'const
                        T**' (in both C and C++) is explained well in the C++ FAQ



                        This is C++ FAQ, but the reasoning applies immediately to C as well. In
                        short, this conversion, if it was allowed, would create a large hole in
                        the "wall" of const-correctness checks :) Sometimes it might appear to
                        be illogical though.

                        It might be worth noting that C++ allows implicit 'T**' -> 'const T*
                        const*' conversion. C doesn't.

                        --
                        Best regards,
                        Andrey Tarasevich

                        Comment

                        Working...