Re: integer to pinter conversion




Jack Klein wrote:
> On Wed, 14 Sep 2005 09:37:14 +0200, Irrwahn Grausewitz
> <irrwahn35@xxxxxxxxxx> wrote in comp.lang.c:
>
> > junky_fellow@xxxxxxxxxxx wrote:
> > >Consider a function:
> > >
> > >void *test_func(void)
> > >{
> > > return ((void *)-1);
> > >}
> > >
> > >While returning, the integer -1 is converted to void *.
> > >Is this portable ?
> >
> > No. While an integer may be converted to a pointer, the result
> > of this conversion is implementation-defined, with the obvious
> > exception of an integer constant expression with the value 0.
> >
> > FWIW, you don't even need the cast in the code above.
>
> I beg to differ. With the exception of an integer constant expression
> evaluating to 0 (i.e., special case to initialize/set a pointer to
> NULL), a cast is required to convert between an integer type and a
> pointer type. Regardless of whether that is in an initialization,
> assignment, or passing as a parameter or returning. The latter two
> are performed "as if by assignment" anyway.
>

Since the return type is void *, so if the function func() returns
-1, will the compiler not convert the integer to void * even if
the cast is not applied ? I get a warning without the cast, but the
conversion is still being done.

.



Relevant Pages

  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... You can't do pointer arithmetic on a void* value. ... qsort behaves in a manner consistent with its specification. ... actually works as advertised...but doesn't mean that the cast is ...
    (comp.lang.c)
  • Re: C variable retyping
    ... whose type is given by the cast. ... >an integer, no conversion is done, and c is used, within the ... In other words, you may take a pointer value, then use a cast to ... which is probably why the C99 folks decided to allow it ...
    (comp.lang.c)
  • Re: dynamic_cast does not work as specified
    ... then p is a pointer to an object of type SubThing. ... SubThing* via a C-style cast. ... virtual void ShowPURE; ... type-id must be a derived class of expression [note that the simple explanation could be ...
    (microsoft.public.vc.mfc)
  • Re: [RFC] timers, pointers to functions and type safety
    ... * they have callback of type void ... callback is called by the code that even in theory has no ... cast to unsigned long and cast back in the callback. ... number - not a pointer cast to unsigned long, not an index in array, etc. ...
    (Linux-Kernel)
  • Re: About casts (and pointers)
    ... > a cast). ... > the implementation's (void **) representation and length. ... Your sometype ** pointer is presumably pointing at ...
    (comp.lang.c)