Discussion: example rquired
Afficher un message
Vieux 21/10/2007, 08h47   #4
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: example rquired

Jack Klein <jackklein@spamcop.net> writes:
> On Sun, 21 Oct 2007 03:21:53 -0000, siju <aarklon@gmail.com> wrote in
> comp.lang.c:
>> the following post was saying that
>> http://groups.google.com/group/comp....start=30&sa=N&
>>
>> The language doesn't allow conversions from floating-point to poitner
>> types, either implicitly or explicitly.

>
> That is almost correct. The C standard allows the conversion, with a
> suitable cast, between pointers and integer types. Conversion of a
> pointer to an integer has implementation-defined behavior if the
> result of the conversion fits in the destination integer type,
> undefined behavior otherwise.
>
> If an implementation has an integer type capable of holding a pointer
> value, conversion of a pointer value to this integer type, via a cast,
> and then back again to a pointer to the original type, again via a
> cast, will result in a pointer that points to the same object.
>
> But you can convert from a floating point type to a pointer type using
> two casts, of course.
>
> double d = 3.14159;
> void *vp = (void *)((int)d);
>
> So if you want to be completely correct, you would have to say that
> you can't directly convert between floating point and pointer types,
> implicitly or explicitly.

[...]

Are you sure about that? What constraint does it violate?

C99 6.5.4, "Cast operators", constrains the source and target types to
be scalar (unless the target is void). C99 6.3, "Conversions",
doesn't define the behavior of conversions between floating-point and
pointer types, but there are no constraints in that section.

As far as I can tell, conversion between a pointer type and a
floating-point type is legal, but the behavior is undefined. (IMHO it
would make more sense if it were illegal, but the standard doesn't
seem to say so.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
 
Page generated in 0,05274 seconds with 9 queries