View previous topic :: View next topic |
Author |
Message |
garyzheng217 Guest
|
Union and structure? |
Posted: Thu Jul 22, 2004 9:15 am |
|
|
Could some one told me how to nested the structure into Union
And could some one give out some samples to me?
union { congiguration for shift register
char ShiftReg;
struct {
unsigned char ShiftRegLSB :1; // less significant bit'
unsigned char s1:1;
unsigned char s2:1;
unsigned char s3:1;
unsigned char s4:1;
unsigned char s5:1;
unsigned char s6:1;
unsigned char ShiftRegMSB :1; //most significant bit
} ;
};
i write the program like this, but the CCS compiler can not recoginize the
the structure of shiftreg
please give me some hint:) |
|
|
Ttelmah Guest
|
Re: Union and structure? |
Posted: Thu Jul 22, 2004 10:26 am |
|
|
garyzheng217 wrote: | Could some one told me how to nested the structure into Union
And could some one give out some samples to me?
union { congiguration for shift register
char ShiftReg;
struct {
unsigned char ShiftRegLSB :1; // less significant bit'
unsigned char s1:1;
unsigned char s2:1;
unsigned char s3:1;
unsigned char s4:1;
unsigned char s5:1;
unsigned char s6:1;
unsigned char ShiftRegMSB :1; //most significant bit
} ;
};
i write the program like this, but the CCS compiler can not recoginize the
the structure of shiftreg
please give me some hint:) |
You don't declare a varable name for the structure definition (only inside). So how is it going to be referred to in the structure?. This won't work in other C's either.
Use something like:
struct bits {
int8 LSB:1;
int8 s1:1;
int8 s2:1;
int8 s3:1;
int8 s4:1;
int8 s5:1;
int8 s6:1;
int8 MSB:1;
};
union {
char ShiftReg;
struct bits bitaccess;
} value;
Then value.ShiftReg, is the whole register, while value.bitaccess.LSB, refers to the LSB.
Best Wishes |
|
|
garyzheng
Joined: 22 Jul 2004 Posts: 25
|
Re: Union and structure? |
Posted: Thu Jul 22, 2004 10:49 am |
|
|
Ttelmah wrote: | garyzheng217 wrote: | Could some one told me how to nested the structure into Union
And could some one give out some samples to me?
union { congiguration for shift register
char ShiftReg;
struct {
unsigned char ShiftRegLSB :1; // less significant bit'
unsigned char s1:1;
unsigned char s2:1;
unsigned char s3:1;
unsigned char s4:1;
unsigned char s5:1;
unsigned char s6:1;
unsigned char ShiftRegMSB :1; //most significant bit
} ;
};
i write the program like this, but the CCS compiler can not recoginize the
the structure of shiftreg
please give me some hint:) |
You don't declare a varable name for the structure definition (only inside). So how is it going to be referred to in the structure?. This won't work in other C's either.
Use something like:
struct bits {
int8 LSB:1;
int8 s1:1;
int8 s2:1;
int8 s3:1;
int8 s4:1;
int8 s5:1;
int8 s6:1;
int8 MSB:1;
};
union {
char ShiftReg;
struct bits bitaccess;
} value;
Then value.ShiftReg, is the whole register, while value.bitaccess.LSB, refers to the LSB.
Best Wishes |
thank you very much:) i got your point:) |
|
|
|