strange warnings passing int* to const int*

Discussion in 'C Programming' started by george doubleu, Jul 9, 2004.

  1. hi,

    i'm using "gcc version 3.3.3 (Debian)" to compile the program pasted below.
    I get the two warnings you can see in the remarks. The second warning is
    perfectly OK for me, but the first one I don't understand.

    Isn't the "const int *" construct in the signature of func1 a hint for the
    user, that func1 doesn't modify **arg? Why then is it dangerous to
    pass an alterable argmument?

    thanks for your help!

    ----------------------------------------------------
    void func1(const int *arg[]) { }

    void func2(int *arg[]) { }

    int main(argc)
    {
    const int *ary1[10];
    int *ary2[10];

    func1(ary1);
    func1(ary2); // warning: passing arg 1 of `func1' from incompatible pointer type
    func2(ary1); // warning: passing arg 1 of `func2' from incompatible pointer type
    func2(ary2);
    }
    ----------------------------------------------------
    george doubleu, Jul 9, 2004
    #1
    1. Advertising

  2. On Fri, 9 Jul 2004, george doubleu wrote:
    >
    > i'm using "gcc version 3.3.3 (Debian)" to compile the program pasted below.
    > I get the two warnings you can see in the remarks. The second warning is
    > perfectly OK for me, but the first one I don't understand.
    >
    > Isn't the "const int *" construct in the signature of func1 a hint for the
    > user, that func1 doesn't modify **arg? Why then is it dangerous to
    > pass an alterable argument?



    > void func1(const int *arg[]) { }
    >
    > int *ary2[10];
    >
    > func1(ary2); // warning: passing arg 1 of `func1'
    > // from incompatible pointer type



    'ary2' is an 'array[10] of pointer to int'. It decays in this context
    to a 'pointer to pointer to int'. That is, it is the address of a
    'pointer to int'.

    'func1' expects a parameter of type 'pointer to pointer to const int'.
    That is, it expects the address of a 'pointer to const int'.

    'pointer to int' and 'pointer to const int' are two different
    types, in C. Pointers to different types are not compatible. Thus
    'pointer to (pointer to int)' is not compatible with 'pointer to
    (pointer to const int)'. Thus GCC gives you a warning.

    How to fix it? See another thread in this newsgroup about
    improper const-ifying's leading to type errors (Passing const void*
    to free, or some such). Also Google for "const poisoning."
    The bottom line is that this kind of error can look bizarre, but
    it never occurs in well-written, real-life code, so just accept
    it and you'll never see it again. :)

    -Arthur
    Arthur J. O'Dwyer, Jul 9, 2004
    #2
    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. Timo Freiberger
    Replies:
    3
    Views:
    941
    Bob Hairgrove
    Oct 30, 2004
  2. ThazKool
    Replies:
    1
    Views:
    444
  3. Replies:
    11
    Views:
    1,099
  4. Javier
    Replies:
    2
    Views:
    558
    James Kanze
    Sep 4, 2007
  5. 0m
    Replies:
    26
    Views:
    1,110
    Tim Rentsch
    Nov 10, 2008
Loading...

Share This Page