yellowcorn yellowcorn - 3 months ago 12
C Question

C Structure initialization (Array of integers and an integer value)

#define MAX 10
struct setArray
{
int item[MAX];
int count;
};
typedef struct setArray *BitSet;


how should I initialize the elements in the structure?

Answer

For example

struct setArray s = { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, MAX };

Or

struct setArray s;

for ( int i = 0; i < MAX; i++ ) s.item[i] = i;
s.count = MAX;

Or

BitSet p = malloc( sizeof( *p ) );

for ( int i = 0; i < MAX; i++ ) p->item[i] = i;
p->count = MAX;