How can I create a bit-field array with variable size? The following code is what I tried, which didn't work.
#include <stdio.h>
int main()
{
int n=4;
struct bite{
unsigned a1:2;
unsigned a2:2;
:
:
unsigned a(n-1):2;
unsigned a(n):2;
}bits;
for(i=1;i<=n;i++)
bits.a[i]=i;
for(i=1;i<=n;i++)
printf("%d ",bits.a[i]);
return 0;
}
The members of a
struct
cannot be defined at runtime.You could simulate a bit array using a
char
array and some macros.Similar for 2-bit words are per your code: