简体   繁体   中英

What do these struct and typedef definitions mean?

I have the following code, which works fine and has been in use for a quite a while now... but I have no idea what it means.

struct event_param
{
    int task:3;
    int param1;
    int param2;
};

#define SV_DRIVER_EVENTS_MASK_SIZE (SV_DRIVER_EVENT_LAST*sizeof(struct event_param))
typedef struct event_param driver_event_mask[SV_DRIVER_EVENTS_MASK_SIZE];
typedef driver_event_mask DriverEventMask;
  • What does driver_event_mask represent?
  • Why is there sizeof(struct event_param) inside the array?

The :3 thing represents a bit field . I'm not sure it matters much, though, since sizeof(struct event_param) will still almost certainly be 12 bytes, I think.

So what happens here is that an array of SV_DRIVER_EVENT_LAST of event_param structures is created. But I would also expect that the sizeof(struct event_param) is not necessary here -- the length of an array is defined in the number of units it contains, not in bytes.

1. driver_event_mask is just what it says. A type to represent an array of SV_DRIVER_EVENTS_MASK_SIZE struct event_param items.

2. sizeof(struct event_param) is not needed here, but doesn't hurt (ie won't cause bugs) since it just means you allocate about 12 times as much memory as you actually need.

how to typedef strcuts array in c?

How to define is already shown in your question, for why refer below:-

If a typedef name denotes a variable length array type, the length of the array is fixed at the time the typedef name is defined, not each time it is used:

what does DriverEventMask represents

DriverEventMask represent struct event_param

why there are sizeof(struct event_param)

So that SV_DRIVER_EVENTS_MASK_SIZE will represent the size of struct event_param. This struct contain bit fields and the way it is arranged is not sure until we know if there are any compiler directives to avoid padding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM