Consider below code, I have written:
#include <stdio.h>
#include <stdint.h>
union myAccess {
uint16_t access16;
struct {
uint8_t lo;
uint8_t hi;
} access8;
};
union myByte{
uint8_t 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;
}BIT;
};
int main()
{
union myAccess U;
U.access8.lo=0xF1;
U.access8.hi=0x55;
printf("%x\n",U);
union myByte B;
B.BYTE=0;
B.BIT.BIT4=1;
printf("%x\n",B);
return 0;
}
The output is:
Gaurav@Gaurav-PC /cygdrive/d
$ ./LSI
2255f1
61279210
Now when I modify my code as below:
#include <stdio.h>
#include <stdint.h>
union myAccess {
uint16_t access16;
struct {
uint8_t lo;
union myByte hi;//here
} access8;
};
union myByte{
uint8_t 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;
}BIT;
};
int main()
{
union myAccess U;
U.access8.lo=0xF1;
U.access8.hi.BYTE=0x55;
printf("%x\n",U);
return 0;
}
It is showing compilation error at here
Gaurav@Gaurav-PC /cygdrive/d
$ gcc -Wall LSI.c -o LSI
LSI.c:8: error: field `hi' has incomplete type
LSI.c: In function `main':
LSI.c:33: warning: unsigned int format, myAccess arg (arg 2)
LSI.c:33: warning: unsigned int format, myAccess arg (arg 2)
What am I doing wrong?