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

H

Heiko Vogel

**** 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
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
V

Vyom

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

j

Vyom said:
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.


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;
}
 
M

Method Man

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top