Passing pointer array to function

Discussion in 'C Programming' started by Tatu Portin, Dec 12, 2004.

  1. Tatu Portin

    Tatu Portin Guest

    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_comments);



    return 0;
    }

    GCC (3.1) would say that
    >warning: passing arg 1 of 'func' from incompatible pointer type

    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
    Tatu Portin, Dec 12, 2004
    #1
    1. Advertising

  2. Tatu Portin wrote:

    > I have a struct:
    >
    > cat main.c

    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_comments);

    return 0;
    }

    > gcc -Wall -ansi -pedantic -ffast-math -O2 -o main main.c
    > gcc --version

    gcc (GCC) 3.4.1

    >
    > GCC (3.1) would say that
    > >warning: passing arg 1 of 'func' from incompatible pointer type


    It seems to compile just fine for me.
    E. Robert Tisdale, Dec 12, 2004
    #2
    1. Advertising

  3. Tatu Portin

    Mike Wahler Guest

    "Tatu Portin" <> wrote in message
    news:TENud.449$...
    > 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_comments);
    >
    >
    >
    > return 0;
    > }
    >
    > GCC (3.1) would say that
    > >warning: passing arg 1 of 'func' from incompatible pointer type

    >
    > But still my code in 'func' (not posted,


    Why not?

    > 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?


    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
    Mike Wahler, Dec 12, 2004
    #3
  4. Tatu Portin

    Joe Wright Guest

    Tatu Portin wrote:
    > 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_comments);
    >
    >
    >
    > return 0;
    > }
    >
    > GCC (3.1) would say that
    > >warning: passing arg 1 of 'func' from incompatible pointer type

    >
    > 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


    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>gcc --version
    gcc.exe (GCC) 3.1
    Copyright (C) 2002 Free Software Foundation, Inc.

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Dec 12, 2004
    #4
  5. Tatu Portin

    Tatu Portin Guest

    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_comments, vc->comments);

    71: val = strpbrk (vc->user_comments[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_comments_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;
    Tatu Portin, Dec 12, 2004
    #5
  6. On Sun, 12 Dec 2004 03:43:16 GMT, Tatu Portin <> wrote:

    >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_comments, vc->comments);


    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.
    Raymond Martineau, Dec 12, 2004
    #6
  7. Tatu Portin

    Chris Torek Guest

    In article <o7Pud.3$> Tatu Portin <> wrote:
    >Ok. First post was wrong. (I thought that simplifying would do the same. It
    >didn't.)


    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.
    Chris Torek, Dec 12, 2004
    #7
  8. Tatu Portin

    CBFalconer Guest

    Mike Wahler wrote:
    >
    > "Tatu Portin" <> wrote in message
    > news:TENud.449$...
    > > 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_comments);
    > >
    > >
    > >
    > > return 0;
    > > }
    > >
    > > GCC (3.1) would say that
    > > >warning: passing arg 1 of 'func' from incompatible pointer type

    > >
    > > But still my code in 'func' (not posted,

    >
    > Why not?
    >
    > > 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?

    >
    > 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.


    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Dec 12, 2004
    #8
  9. Tatu Portin

    Chris Torek Guest

    In article <>
    Raymond Martineau <> wrote:
    >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 ...


    Actually, it is *not* valid in C either.

    >but GCC decides it's worth raising a warning about converting
    >'char**' to 'const char**'.


    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).

    >You can get rid of the warning by doing a type cast, but there may
    >be better methods of dealing with it.


    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.
    Chris Torek, Dec 12, 2004
    #9
  10. Tatu Portin

    Mike Wahler Guest

    "CBFalconer" <> wrote in message
    news:...
    > Mike Wahler wrote:
    > >
    > > 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.

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


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

    -Mike
    Mike Wahler, Dec 12, 2004
    #10
  11. Tatu Portin

    Tatu Portin Guest

    Tatu Portin wrote:
    > 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_comments, vc->comments);
    >
    > 71: val = strpbrk (vc->user_comments[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_comments_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;



    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')
    Tatu Portin, Dec 12, 2004
    #11
  12. Tatu Portin wrote:
    > ...
    > But is anyone capable of saying why exactly this fails? (with 'const char **table')
    > ...


    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

    http://www.parashift.com/c -faq-lite/const-correctness.html#faq-18.15

    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
    Andrey Tarasevich, Dec 12, 2004
    #12
    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. James Vanns
    Replies:
    7
    Views:
    7,035
    Evan Carew
    Jan 21, 2004
  2. entitledX
    Replies:
    4
    Views:
    513
    Old Wolf
    Jan 12, 2006
  3. jimjim
    Replies:
    16
    Views:
    837
    Jordan Abel
    Mar 28, 2006
  4. Vijai Kalyan
    Replies:
    4
    Views:
    704
    Vijai Kalyan
    Nov 8, 2005
  5. PeterOut

    Passing a 2D array as a pointer to a pointer

    PeterOut, Aug 27, 2008, in forum: C Programming
    Replies:
    3
    Views:
    305
    Nick Keighley
    Aug 28, 2008
Loading...

Share This Page