comp.lang.c
[Top] [All Lists]

void * vs. T *

Subject: void * vs. T *
From: Ark Khasin
Date: Thu, 27 Mar 2008 06:30:33 GMT
Newsgroups: comp.lang.c


Is there a way to assert at compile time that T* and void* have identical representations, so the build breaks if the assumption is invalid? IOW, is there a constant expression for that?

<Background>
In a particular situation, it makes code more compact and comprehensible with a hack of the sort
Ty myfunc(T *arg)
{
        //something
}
..............
//in fact, a const array of structs of those:
Ty (*pFunc)() = myfunc;
void *pData = &Sometype_data;
..............
//and the hacky call
        Ty x = pFunc(pData);

The hack works if T* and void* happen to have identical representations.
</Backgound>

E.g.
(uintmax_t)(void*)123==(uintmax_t)(T*)123 && sizeof(void*)==sizeof(T*)
seem not to guarantee anything.
Like, can a malicious implementer bit-invert and reverse the order of bits in void* and char* bit patterns, leaving other pointer types natural for the platform?

--
Thanks,
Ark

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy