When creating an array of a bit field structure as in the example below, is it possible to fit two 4-bit array members into a single byte?
typedef struct {
unsigned int my_location : 2;
unsigned int my_WATCHDOG : 2;
} foo;
foo my_array[1000] = 0;
Is it possible to have the above result in 500 bytes of 1000 elements, each addressed by my_array[i] or will CW pad every element such that 1000 bytes will be used by the array?
Thanks for the help,
drdr
Solved! Go to Solution.
No, not with a simple foo structure.
C requires that array elements have their own addresses, therefore in order to have such a array you will have to pack a single byte in C code.
typedef struct {
unsigned char my_low_location : 2;
unsigned char my_low_WATCHDOG : 2;
unsigned char my_high_location : 2;
unsigned char my_high_WATCHDOG : 2;
} foo;
foo my_array[500];
Alternatively, and you can also use an array of bytes and then work with macros to implement the accesses to have a direct indexed access.
No, not with a simple foo structure.
C requires that array elements have their own addresses, therefore in order to have such a array you will have to pack a single byte in C code.
typedef struct {
unsigned char my_low_location : 2;
unsigned char my_low_WATCHDOG : 2;
unsigned char my_high_location : 2;
unsigned char my_high_WATCHDOG : 2;
} foo;
foo my_array[500];
Alternatively, and you can also use an array of bytes and then work with macros to implement the accesses to have a direct indexed access.
Hi Daniel,
Thanks for the tip.
drdr