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


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] */


Thanks,
--Matt



> -----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.
> 
> 


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