This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]