How to convert a double **ptr in a double const ** const ptr?

Discussion in 'C Programming' started by Heiko Vogel, Sep 12, 2004.

  1. Heiko Vogel

    Heiko Vogel Guest

    **** Post for FREE via your newsreader at post.usenet.com ****

    Hi newsgroup,
    can anybody tell me, why the following code snippet won't compile:

    double **ptr;
    double const ** const c_ptr = ptr;

    I always have to cast it, to let it work correctly:

    double **ptr;
    double const ** const c_ptr = (double const ** const) ptr;

    But it makes me wonder that the following code snippet works without a
    cast, though I am doing "nearly" the same as above:

    double *ptr;
    double const * const ptr = c_ptr;

    These lines are perfectly accepted by the compiler -- I don't need to
    cast. But why it doesn't work with ** pointers?


    Thank you very much,
    Heiko Vogel

    P.S: I am using the GNU C Compiler (gcc-3.3.2)

    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    *** Usenet.com - The #1 Usenet Newsgroup Service on The Planet! ***
    http://www.usenet.com
    Unlimited Download - 19 Seperate Servers - 90,000 groups - Uncensored
    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    Heiko Vogel, Sep 12, 2004
    #1
    1. Advertising

  2. Heiko Vogel

    Vyom Guest

    > double *ptr;
    > double const * const ptr = c_ptr;


    This is possible because:
    According to the standard
    - Both operands are pointers to qualified or unqualified versions of
    compatible types, and the type pointed to by the left has all the
    qualifiers of the type pointed to by the right.

    > double **ptr;
    > double const ** const c_ptr = ptr;


    But this is not possible as the left side is

    "pointer to (const qualified pointer to double)" and the right is
    "pointer to (pointer to double)"

    thus they differ in the thing /immediately/ pointed to
    and therefore are not compatible.

    --
    Vyom
    Vyom, Sep 13, 2004
    #2
    1. Advertising

  3. Heiko Vogel

    j Guest

    "Vyom" <> wrote in message
    news:...
    > > double *ptr;
    > > double const * const ptr = c_ptr;

    >
    > This is possible because:
    > According to the standard
    > - Both operands are pointers to qualified or unqualified versions of
    > compatible types, and the type pointed to by the left has all the
    > qualifiers of the type pointed to by the right.
    >
    > > double **ptr;
    > > double const ** const c_ptr = ptr;

    >
    > But this is not possible as the left side is
    >
    > "pointer to (const qualified pointer to double)" and the right is


    That is a read-only pointer to pointer to read-only double

    or to look at it more clearly

    (double (const (* (* (const c_ptr)))))

    Start with the inner-most parens and work your way out;
    substitute ``*'' with ``pointer-to'' and ``const'' with ``read-only''

    Additionally -- to demonstrate,

    #include <stdio.h>

    int main(void)
    {
    const double z = 10.0;
    double const *y = &z;
    double const **const x = &y;

    printf("%f\n", **x);

    return 0;
    }


    > "pointer to (pointer to double)"
    >
    > thus they differ in the thing /immediately/ pointed to
    > and therefore are not compatible.
    >
    > --
    > Vyom
    j, Sep 13, 2004
    #3
  4. Heiko Vogel

    Method Man Guest

    > But it makes me wonder that the following code snippet works without a
    > cast, though I am doing "nearly" the same as above:
    >
    > double *ptr;
    > double const * const c_ptr = ptr;
    >


    Fixed (swapped 'ptr' and 'c_ptr' on 2nd line).
    Method Man, Sep 14, 2004
    #4
    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. Sid
    Replies:
    5
    Views:
    1,063
  2. Sydex
    Replies:
    12
    Views:
    6,450
    Victor Bazarov
    Feb 17, 2005
  3. franco ziade

    const ptr to const ptr ?

    franco ziade, Feb 17, 2005, in forum: C Programming
    Replies:
    3
    Views:
    384
    franco ziade
    Feb 17, 2005
  4. G Fernandes
    Replies:
    9
    Views:
    570
    DHOLLINGSWORTH2
    Feb 27, 2005
  5. Jason

    difference between *ptr++ and ++*ptr ?

    Jason, May 15, 2005, in forum: C Programming
    Replies:
    19
    Views:
    6,507
    Chris Torek
    May 19, 2005
Loading...

Share This Page