Re: Question about void pointers



Keith Thompson <kst-u@xxxxxxx> writes:

CBFalconer <cbfalconer@xxxxxxxxx> writes:
[...]
In addition, as far as I can tell, there is no prohibition against
returning void* and char* pointers in different registers (or other
entities). If this is correct, using the interchangeably can
create an ugly code block.

There is no such prohibition in the normative text of the standard,
but it's suggested in a footnote.

C99 6.2.5p27:

A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type.

And a footnote:

The same representation and alignment requirements are meant to
imply interchangeability as arguments to functions, return values
from functions, and members of unions.

This is an unfortunate flaw in the standard.

I think you're misunderstanding the idea behind the footnote.
The "interchangeability" of char*/void* is meant to talk about
values of those types used in particular contexts, not about
derived types with those types in them. For example,

char *
xyzzy(){
void *r = "answer";
return * (char**) &r;
}

is the sort of case the footnote means to address. This
interchangeability does not extend to derived types, such as
trying to convert &xyzzy to (void *(*)()). There's a similar
relationship between int and unsigned int; as values, int and
unsigned int are (often) interchangeable, but pointers to int and
pointers to unsigned int don't bear any special relationship to
each other -- they don't even have to be the same size, for
example. The analogy with function types that have int or
unsigned int in them should be clear.
.



Relevant Pages

  • Problem with function pointers AVR simulation
    ... i find the function pointers do not point ... typedef void; ... bool call_bindfunc; ... unsigned int cmd,prev1_cmd,prev2_cmd; ...
    (comp.arch.embedded)
  • Re: Unions Redux
    ... of course there are pointers involved. ... union U {double a; unsigned int b;} u; ... valid and aliasing rules don't work here. ...
    (comp.lang.c)
  • Re: Hooking D3Device8 by replacing the VTable fails. Info needed.
    ... pointers. ... int,struct IDirect3DSurface9 *) unsigned long * ... *,struct tagRECT const *,unsigned long) unsigned long * ... _D3DPRIMITIVETYPE,unsigned int,void const *,unsigned int) unsigned long * ...
    (microsoft.public.win32.programmer.directx.graphics)
  • Re: Simple question about pointer types
    ... > address directly which obviuosly is of type unsigned int. ... > concerned.The size of unsigned int varies from platform to platform, ... > correspondingly the size of the pointers change. ...
    (comp.lang.c)
  • Re: why this program is not crashing
    ... >>I have no standard on my fingertips right now but IIRC for ... pointers are passed as whatever type they happen to be. ... > converted to int or unsigned int, and float is converted to double. ... It's best, IMHO, to use an explicit cast even in that ...
    (comp.lang.c)