This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Avoiding "assignment from incompatible pointer type" warning
- From: John Love-Jensen <eljay at adobe dot com>
- To: Claudio Bley <bley at cs dot uni-magdeburg dot de>, Florian Weimer <fw at deneb dot enyo dot de>
- Cc: Joshua Nye <josh at boxcarmedia dot com>, Steve Dondley <s at dondley dot com>, <gcc-help at gcc dot gnu dot org>
- Date: Mon, 21 Oct 2002 08:40:48 -0500
- Subject: Re: Avoiding "assignment from incompatible pointer type" warning
Hi Claudio,
> I can't see why. Can you elaborate on that? I mean, casting on the
> machine code level does just nothing - it's just "syntactic sugar" to
> convince the compiler to be quiet. I think casting from one pointer
> type to another does no harm at all. Am I wrong?
You are correct, the casting is fine, insofar as the given code snippet
goes.
I assume the complaint/concern was regarding the (presumed) dereferencing of
the int* p at some later point in the code.
Note: cavalier casting is highly suspect, and prone to be very platform
specific (when it does work as intended). For instance, on Solaris a
misaligned value can cause a bus error. Often the injudicious, cavalier
casting is the culprit.
To do the hex dump as intended, I'd do something like this...
void HexDump(void* ptr, size_t len)
{
unsigned char* p = (unsigned char*)ptr;
while(len--)
{
printf("%02x%c", *p++, len ? ' ' : '\n');
}
}
int main()
{
float g = 3.141592653589793238;
HexDump(&g, sizeof g);
}
...but that's just me. :-)
--Eljay