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

Struct in a union

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







Struct in a union
PostPosted: Thu Aug 10, 2006 4:36 am     Reply with quote

Hello there.

I'm having a difficulties with Struct and Union. I would like to create a struct as a member of a union. Here is my code:

#include <18F452.h>

#include "typedefs.h"
#use delay(clock=10000000)

typedef union
{
int8 _byte;
struct
{
unsigned bit0:1;
unsigned bit1:1;
unsigned bit2:1;
unsigned bit3:1;
unsigned bit4:1;
unsigned bit5:1;
unsigned bit6:1;
unsigned bit7:1;
};
} RESPONSE_1;

typedef union
{
int8 _byte;
struct
{
unsigned bit0:1;
unsigned bit1:1;
unsigned bit2:1;
unsigned bit3:1;
unsigned bit4:1;
unsigned bit5:1;
unsigned bit6:1;
unsigned bit7:1;
};
} RESPONSE_2;

int main(void)
{
RESPONSE_1 response;
response.bit0= 0;

RESPONSE_2 response2;
response2.bit0= 0;
return 1;
}

The compilers says that bit0 is not a member of "reponse" and "response 2". I'm using PCH version 3.170.
I tested my code in Visual studio and the compiler didn't complain.

Am I doind anything wrong?

Thank in advance.

Christophe D
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Aug 10, 2006 6:00 am     Reply with quote

Strange, v3.187 and v3.236 of the compiler give me an error of 'Element is not a member', but version 3.249 compiles without error.
Thing is that I think the older compiler versions are correct and this is an error in v3.249.... maybe it depends on which C standard you are using, K&R or ANSII ?

The way I learned to program this was that you can not have unnamed structs within the union:
Code:
typedef union
{
  int8 _byte;
  struct
  {
    int8 bit0:1;
    int8 bit1:1;
    int8 bit2:1;
    int8 bit3:1;
    int8 bit4:1;
    int8 bit5:1;
    int8 bit6:1;
    int8 bit7:1;
  } MyBits;     // <---  Added a name for the struct
} RESPONSE_1;

void main(void)    // Changed to void, returning a value has no use and is waste of program memory.
{
  RESPONSE_1 response;
  response.MyBits.bit0 = 0;
}
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