question about cast

Y

Yacine

Does anyone know why

void main(){
double x;
double *y;
(int) y = x;
}

compiles well, and why

void main(){
double x;
double *y;
(double) y = x;
}

does not compile well ?!?
Why is it not allowed to cast a pointer to a double in the seconde case ?
Thanks
Yacine
 
R

Richard Bos

Yacine said:
Does anyone know why

void main(){
double x;
double *y;
(int) y = x;
}

compiles well,

Because your implementation is broken, or you're calling it the wrong
way. main() returns int. The cast and subsequent assignment are about as
dubious as Bush Junior's veracity, btw.
void main(){
double x;
double *y;
(double) y = x;
}

does not compile well ?!?

Because casting a pointer to int is dubious, but possible, while casting
a pointer to double makes no sense at all.

Richard
 
Y

Yacine

Richard said:
Because your implementation is broken, or you're calling it the wrong
way. main() returns int. The cast and subsequent assignment are about as
dubious as Bush Junior's veracity, btw.




Because casting a pointer to int is dubious, but possible, while casting
a pointer to double makes no sense at all.

Richard
The return type of main has nothing to do with the problem I'm talking
about. My question is: is it possible to cast a pointer to a double ? I
think it does not make more sense to cast a pointer to int than casting
a pointer to double ...
 
P

pete

Yacine said:
Does anyone know why

void main(){
double x;
double *y;
(int) y = x;
}

compiles well,

Because you are not using a conforming implementation of C.
A cast on the left operand of the assignment operator
is a constraint violation.
 
Y

Yacine

pete said:
Because you are not using a conforming implementation of C.
A cast on the left operand of the assignment operator
is a constraint violation.
How can I handle that problem ?
 
P

pete

Yacine said:
How can I handle that problem ?

If you have made sure that there are no alignment problems
and no size problems, then you can

*(int *)&y = x;

If you haven't made sure, then don't do that.
 
R

Richard Heathfield

Yacine said:
Does anyone know why

void main()

int main(void)

{
double x;
double *y;
(int) y = x;
}

compiles well,

You can't assign to a value, only to an object. (int)y is a value, not an
object. You should get a diagnostic. Turn up your warning level.
and why

void main()

int main(void)

{
double x;
double *y;
(double) y = x;
}

does not compile well ?!?

Same reason the first one shouldn't. You can't assign to a value, only to an
object. (double)y is a value, not an object.
 
P

pete

pete said:
If you have made sure that there are no alignment problems
and no size problems, then you can

*(int *)&y = x;

*(int *)&y = (int)x;

Excuse me, I forgot how invloved it was.
 
H

Horst Kraemer

Does anyone know why

void main(){
double x;
double *y;
(int) y = x;
}

This shouldn't compile at all because it isn't C - even if it accepts
"void main()".

In C you can't assign anything to "(int)y" because (int)y is a *value*
and not an object (LValue).
compiles well, and why

void main(){
double x;
double *y;
(double) y = x;
}

does not compile well ?!?

Idem.

Your compiler uses some extension to the C language. It allows
(int)y=x because sizeof(int)==sizeof y on this platform and it refuses
(double)y=x because sizeof(double)>sizeof y on this platform.

In C the first assignment may be written as

*(int*)&y = x;

and the second as

*(double*)&y = x;

but the behaviour of the second assigment is undefined on your
platform because the type of the left operand is double and sizeof
(double) (probably ==8) > sizeof y (probably ==4). The assigment would
write beyond the memory belonging to y. Moreover a double object may
have different aligment requirements than a

Regards
Horst
 
R

Richard Heathfield

Yacine said:
The return type of main has nothing to do with the problem I'm talking
about.

If you don't want people to point out errors in your code, don't post
erroneous code.
 
M

Martin Ambuhl

Yacine said:
Does anyone know why

void main(){
^^^^
incorrect return type for main
double x;
double *y;
(int) y = x;
^^^^
illegal use of a cast where an lvalue is required.
}

compiles well,

"Bullshit," you say, "Bullshit!". The above is hopelessly broken.
and why

void main(){
double x;
double *y;
(double) y = x;
}

does not compile well ?!?

For the same reason the first one does *not* "compile well."
Why is it not allowed to cast a pointer to a double in the seconde case ?

For the same reason that your first example is not allowed.
 
Y

Yacine

Horst said:
This shouldn't compile at all because it isn't C - even if it accepts
"void main()".

In C you can't assign anything to "(int)y" because (int)y is a *value*
and not an object (LValue).




Idem.

Your compiler uses some extension to the C language. It allows
(int)y=x because sizeof(int)==sizeof y on this platform and it refuses
(double)y=x because sizeof(double)>sizeof y on this platform.

In C the first assignment may be written as

*(int*)&y = x;

and the second as

*(double*)&y = x;

but the behaviour of the second assigment is undefined on your
platform because the type of the left operand is double and sizeof
(double) (probably ==8) > sizeof y (probably ==4). The assigment would
write beyond the memory belonging to y. Moreover a double object may
have different aligment requirements than a

Regards
Horst
Thanks, that the answer I was waiting for
 
K

Keith Thompson

Yacine said:
How can I handle that problem ?

Some other languages, perhaps with names starting with 'C' and
containing punctuation characters, may allow a cast as the left
operand of an assignment operator.

You're trying to do something that doesn't make any sense. The
compiler is preventing you from doing it. I don't see a problem.

Seriously, just what are you trying to do?

Here's the code that the compiler complains about:

double x;
double *y;
(double) y = x; /* illegal */

x is a double; y is a pointer to double.

If you want to make y point to x, you can do this:

y = &x;

If you want to make y point to a float variable with the value of x,
without changing the memory location to which y points, you can do this:

*y = x;

but only after initializing y so it points to a valid memory location.
 
C

Chris Torek

wrote: [snippage]
double x;
double *y;
(int) y = x;

Your compiler uses some extension to the C language.

This much is reasonably clear (the other "likely" possibility is that
his compiler is simply broken :) ).
In C the first assignment may be written as

*(int*)&y = x;

Note that one popular compiler that adds a "cast as lvalue" rule,
the GNU C Compiler (gcc), defines cast-as-lvalue *very* differently.
The assignment to (int)y is not equivalent to the above, but rather
to the (syntactically valid but semantically dodgy) ANSI/ISO C code:

(int)(y = (double *)(int)&x)
and the second as
*(double*)&y = x;

Gcc would define this as:

(double)(y = (double *)(double)&x)

which would then draw a complaint ("pointer value used where a
floating point value was expected").

For more details on gcc's weird sort-of-like-C language GNUC (which
I pronounce as "ganuck", more or less), see "info gcc", assuming
the info files have been installed along with the compiler. The
definition of lvalue-as-cast is under "C Extensions" and then
"Lvalues".
 
E

E. Robert Tisdale

Yacine said:
Horst Kraemer wrote:
[snip]

Thanks, that the answer I was waiting for.

Sorry about your encounters with our "indigenous trolls".
After awhile you'll begin to recognize them and ignore them.
 
K

Keith Thompson

Horst Kraemer said:
Your compiler uses some extension to the C language. It allows
(int)y=x because sizeof(int)==sizeof y on this platform and it refuses
(double)y=x because sizeof(double)>sizeof y on this platform.

I don't believe the sizes of the various types have anything to do
with this.

In C, a cast is not allowed on the left side of an assignment,
regardless of the types. If a cast is being used as a value (not as
an lvalue), it's legal to convert from a pointer type to an integer
type or vice versa, whether the sizes match or not. It's not legal to
convert between pointer types and floating-point types.

I would guess that the extension implemented by whatever compiler the
OP is using (allowing a cast as an lvalue) would following the same
rules. (As I vaguely alluded to earlier, I think C++ allows casts as
lvalues. I don't know the details, which aren't topical anyway.)

Note that conversions between integers and pointers, though they're
legal, are not usually meaningful. You can convert a pointer to a
sufficiently large integer type (if there is one) and back again and
get the same pointer value. Anything else will probably give you
garbage.
 
P

Peter Nilsson

pete said:
*(int *)&y = (int)x;

Excuse me, I forgot how invloved it was.

? The (int) cast is redundant!

'* (int *) &y' forms an lvalue of type int, so the expression x will
be converted to an int on assignment.
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top