Avoiding "assignment from incompatible pointer type" warning

Moore, Mathew L MooreML@BATTELLE.ORG
Sun Oct 13 13:37:00 GMT 2002

You can always use a void pointer,

  const float x = 2.0625;
  const void *p = &x;
  const int *lp = p;

but explicit casting works just as well.

While we are on this topic, is there a portable way to perform the bit-wise
examination of a floating point variable?  What about for an implementation
where sizeof(float) != sizeof(int) (or sizeof(long), etc.).  Is there
something significantly wrong about doing the following?  Will gcc even
allow this?

  union {
    float x;
    char b[sizeof(float)];
  } u;
  u.x = 2.0625;
  /* Access bytes/bits through u.b[0], u.b[1], ..., u.b[sizeof(float)-1] */


> -----Original Message-----
> From: Steve Dondley [mailto:s@dondley.com]
> Sent: Sunday, October 13, 2002 12:13
> To: gcc-help@gcc.gnu.org
> Subject: Avoiding "assignment from incompatible pointer type" warning
> Hi,
> I've written a simple program that print out each bit of a 
> floating point
> variable so I can learn how floating point numbers are represented in
> memory.  The program contains the following statements:
> int *p;
> float g = 2.0625;
> p = &g;
> The third statement above generates an "assignment from 
> incompatible pointer
> type" warning for obvious reasons.  Other than this, the 
> program compiles
> fine and works.
> My question is:  Is there a way to properly assign a pointer 
> of one type to
> a variable of different type so that the warning is suppressed?
> Thanks again.

More information about the Gcc-help mailing list