1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <stdio.h>
typedef struct /*_bitfield_flags*/ bitfield_flags {
unsigned char read : 1;
unsigned char write : 1;
unsigned char unbuf : 1;
unsigned char eof : 1;
unsigned char err : 1;
int : 3; //unused bits
} bitfield_flags;
typedef struct /*_my_iobuf*/ my_FILE {
int cnt;
char* ptr;
char* base;
bitfield_flags bit_flag; /*mode of file access but using bitfields*/
int fd; /*file descriptor*/
} my_FILE;
#define OPEN_MAX 20
my_FILE my_iob[OPEN_MAX] = {
{ 0, (char*)0, (char*)0,{ 1,0,0,0,0 } ,0},
{ 0, (char*)0, (char*)0,{ 0,1,0,0,0 } ,1},
{ 0, (char*)0, (char*)0,{ 0,1,1,0,0 } ,2}
};
void print( bitfield_flags flags, int fd ) {
printf( "{ %u, %u, %u, %u, %u }, %d\n", flags.read, flags.write, flags.unbuf, flags.eof, flags.err, fd ) ;
}
int main()
{
printf( "sizeof(bitfield_flags):%llu\n\n", (unsigned long long)sizeof(bitfield_flags) ) ;
for( int i = 0 ; i < 3 ; ++i ) print( my_iob[i].bit_flag, my_iob[i].fd ) ;
puts("") ;
}
|