CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Anonymous unions: working in 3.249 but broken in 4.071

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Stimpy



Joined: 05 Jan 2009
Posts: 4

View user's profile Send private message

Anonymous unions: working in 3.249 but broken in 4.071
PostPosted: Fri Nov 13, 2009 10:20 am     Reply with quote

I'm trying to port an old project from CCS version 3.249 to 4.071 but I'm running into a problem with anonymous unions. Here's a short example using pic 18F6680:

Code:
struct {
   unsigned int8   update_interval;
   unsigned int8   category;
   union {
   int16      pressure;
   int16      temperature;
   int16      humidity;
   int16      windspeed;
   };
} sensor;


void main() {
unsigned int8 x;
   sensor.category=1;
   sensor.update_interval=60;
   sensor.pressure=1013;
   x=sensor.update_interval;
   printf("X equals %u\n\r",x);
}

In the older 3.249 version, the output is: X equals 60
The same code compiled with 4.071 produces: X equals 245

Has this been fixed in the latest 4.099 release? I would probably renew and upgrade if it has, otherwise does anyone know of any workarounds? The only method I've found so far is to move the union to the beginning of the struct but I don't know if this will be 100% effective in all cases.
I could also give the union a name instead of keeping it anonymous and this does appear to fix things but it's a LOT of code to change. Any other ideas?

Could somebody who has 4.099 try the above example on any PIC18 and see what output you get?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 13, 2009 1:14 pm     Reply with quote

I got this with vs. 4.099:
Quote:
X equals 245

Test program:
Code:

#include <18F6680.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

struct
{
unsigned int8 update_interval;  // 60
unsigned int8 category;   // 1
 
union {
   int16 pressure;   // 1013
   int16 temperature;
   int16 humidity;
   int16 windspeed;
  };

} sensor;

//====================================
void main()
{
unsigned int8 x;

sensor.category=1;
sensor.update_interval=60;
sensor.pressure=1013;

x=sensor.update_interval;

printf("X equals %u\n\r",x);

while(1);
}
RossJ



Joined: 25 Aug 2004
Posts: 66

View user's profile Send private message

PostPosted: Sun Nov 22, 2009 2:51 am     Reply with quote

A similar problem with anonymous struct in a union (4.099 and 4.100). In this case, aaa is located in the same byte as bbb. If the struct is given a name, the problem goes away. I will email CCS support and refer them to this thread.

Code:

union {
   struct {
      int1 aaa;
      int8 bbb;
   };
} xxx;

void main(void)
{
   xxx.aaa = 0;
   xxx.bbb = 1;
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group