Struct and Enum Question

Programming, for all ages and all languages.
Post Reply
Kieran
Member
Member
Posts: 54
Joined: Mon Apr 11, 2005 11:00 pm

Struct and Enum Question

Post by Kieran »

Hi guys, I am creating structures to hold the Floppy Disk Controller information from the HW Ports.

I am wondering; s it possible to crete an enumeration of values (2Bits) within a structure.

Example:

Code: Select all

typedef struct {
        unsigned selected_unit:2;
        unsigned selected head:1;
        unsigned rw_not_ready:1;
        unsigned equipment_check:1;
        unsigned seek_complete:1;
        enum {
             command_successfull=0;
             terminated_abnormally=1;
             invalid_command=2;
             terminated_ready_change=3;
        } last_command_status:2;
} PACKED Command_Status_Register; // 0x3f5 (Read Only)
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Well if nothing else you could just do:

Code: Select all

enum __unnamed {
command_successful=0,
terminated_abnormally=1,
invalid_command=2,
terminated_ready_change=3
};
typedef struct { 
        unsigned selected_unit:2; 
        unsigned selected head:1; 
        unsigned rw_not_ready:1; 
        unsigned equipment_check:1; 
        unsigned seek_complete:1; 
        __unnamed last_command_status:2; 
} PACKED Command_Status_Register; // 0x3f5 (Read Only)
But, to be honest, with the exception of having semicolons where there should be commas in the enum definition I don't see why your code wouldn't compile.
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

Code: Select all

typedef struct {
        unsigned selected_unit:2;
        unsigned selected_head:1;
        unsigned rw_not_ready:1;
        unsigned equipment_check:1;
        unsigned seek_complete:1;
        enum {
             command_successfull=0,
             terminated_abnormally=1,
             invalid_command=2,
             terminated_ready_change=3,
        } last_command_status:2;
} __attribute__((packed)) Command_Status_Register; // 0x3f5 (Read Only) 
compiled fine under gcc
Author of COBOS
Post Reply