Re: gcc bug with const on multidimentional arrays ?

Discussion in 'C Programming' started by cedric, Dec 29, 2004.

  1. cedric

    cedric Guest

    <Context>
    $ cat > glop.c <<EOF

    int titi(const int (*i)[1]) {
    return i[0][0];
    }
    int main(void) {
    int i[][1] = {{1}};
    titi(i); /* line 6 is here */
    return 0;
    }

    EOF
    $ gcc -Wall -pedantic -std=c99 -c glop.c
    glop.c: In function `main':
    glop.c:6: warning: passing arg 1 of `titi' from incompatible pointer type
    </Context>

    Michael Mair wrote:
    > Nope, it is you who needs to understand why this is a sensible
    > rule. -> comp.lang.c


    OK, by the time I wrote this I opened the standard (at 6.5.16.1.6).

    > If you do not want to understand it, you can "shut up" the compiler
    > with an explicit cast to the required type. This is dangerous.


    It would be dangerous if the parameter were a const int **, but with a
    const int *[1] I can't see any trick to get titi to update a const value.

    Is this actually dangerous when the parameter is a const
    multidimentional array instead of a const **T ?
     
    cedric, Dec 29, 2004
    #1
    1. Advertising

  2. cedric

    jacob navia Guest

    cedric wrote:
    > int i[][1] = {{1}};


    I think that the above code should be changed to
    const int i[][1] = {{1}};
    isn't it?
     
    jacob navia, Dec 30, 2004
    #2
    1. Advertising

  3. cedric

    Michael Mair Guest

    jacob navia wrote:
    > cedric wrote:
    >
    >> int i[][1] = {{1}};

    >
    >
    > I think that the above code should be changed to
    > const int i[][1] = {{1}};
    > isn't it?


    Yep. However, the OP snipped parts of the original discussion
    -- which left my statements in the middle of nothing; the
    original problem was that the program warned about
    an incompatible pointer type. The OP would have accepted this
    for "const int **" as this is potentially dangerous but holds
    that "const int (*)[1]" is just a multidimensional array which
    does not give any danger.

    I referred him to c.l.c for an explanation why this is not a
    bug in the standard but makes sense.


    IOW:
    Why do
    --------------------------------
    int titi (const int (*i)[1]) {
    return i[0][0];
    }

    int main(void) {
    int i[][1] = {{1}};
    titi(i); /* <------- */
    return 0;
    }
    --------------------------------
    and, consistently,
    --------------------------------
    typedef int myintarr[1];

    int titi (const myintarr *i) {
    return i[0][0];
    }

    int main(void) {
    myintarr i[] = {{1}};
    titi(i); /* <------- */
    return 0;
    }
    --------------------------------
    give a warning about incompatible pointer types?
    If we would replace the typedef myintarr by a structure
    containing one int, then one could hold that we have
    essentially the same situation, maybe even the same
    memory layout -- so why does the latter not require a
    declaration as const or a cast whereas the former do?


    Cheers
    Michael
    --
    E-Mail: Mine is a gmx dot de address.
     
    Michael Mair, Dec 30, 2004
    #3
    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. willem
    Replies:
    2
    Views:
    548
    cristian
    Feb 15, 2005
  2. Replies:
    1
    Views:
    413
    Victor Bazarov
    Jan 24, 2005
  3. vertigo
    Replies:
    1
    Views:
    264
    Mathias Panzenboeck
    Dec 10, 2006
  4. Bangalore

    multidimentional array

    Bangalore, Sep 27, 2005, in forum: C++
    Replies:
    7
    Views:
    404
    Marcus Kwok
    Oct 6, 2005
  5. Javier
    Replies:
    2
    Views:
    654
    James Kanze
    Sep 4, 2007
Loading...

Share This Page