On Sun, 21 Oct 2007 00:47:00 -0700, Keith Thompson <kst-u@mib.org>
wrote in comp.lang.c:
> 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?
I don't believe that I said it violated a constraint. Admittedly,
"can't" could mean either a constraint violation or undefined
behavior, and I did not specify which, but either one is good for
"can't" in my view. In any cast, I meant undefined behavior, and not
a constraint violation.
> 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.)
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://c-faq.com/
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html