I'm modeling a LCD hitachi display parallel port connection (for those interested HD44780, in 4 bit mode).
Even though alignment is set to 1,this union still takes up 2 bytes. If I get rid of the msnibble struct (directly put db4 to db7 in the struct in the union) it takes up 1 byte. Can structs take up less than 1 byte if they're inside a union? Is the 2 bytes the result of the msnibble taking up 1 byte plus the nibble (the enable bit,rs bit and the two blank bits), thus making the size 2 bytes?
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t byte_t;//could have used char
#pragma pack(push,1)
typedef union
{
byte_t whole_port :8;
struct
{
byte_t enable :1;
byte_t rs :1;
byte_t :1;
byte_t :1;
struct
{
byte_t db4 :1;
byte_t db5 :1;
byte_t db6 :1;
byte_t db7 :1;
}msnibble;
};
} para_port_t;
#pragma pack(pop)
int main(int argc, char** argv) {
printf("%u\n",sizeof(para_port_t));
return (EXIT_SUCCESS);
}
I'm working from netbeans in ubuntu compiling with gcc. As pragma is a compiler directive I wanted to know if it's the same in Microsoft Visual Studio?