c struct problems???

Associate
Joined
10 Feb 2004
Posts
198
Can anyone help me out on this?

I've defined a structure in c with the following......

struct mystructure
{
unsigned char single_char;
unsigned char array_char[2];
};


so far so good. but then i come to create and initialise one with this.....

static struct mystructure fristinstance = {1,2,3};

that gives me 2 warnings.....
1. near initialization for 'firstinstance.array_char'
2. missing braces around initializer

just out of interest the following is compiled without complaint....
static struct mystructure fristinstance = {1};

anyone??
 
well your storing an integers in chars which may be why its giving warnings. try with ' ' around the letters.
 
Last edited:
you need nested braces.

Ladforce said:
well your storing an integers in chars which may be why its giving warnings. try with ' ' around the letters.

That doesn't really matter here.

example :

struct mystructure
{
unsigned char single_char;
unsigned char array_char[2];
};

int main(void) {
struct mystructure a = { 1, { 2, 3 } };
struct mystructure b[2] = { { 1, { 2, 3 } }, { 4, { 5, 6 } } };

printf("%d %d %d\n", a.single_char, a.array_char[0], a.array_char[1]);
printf("%d %d %d : %d %d %d\n",
b[0].single_char, b[0].array_char[0], b[0].array_char[1],
b[1].single_char, b[1].array_char[0], b[1].array_char[1]);
}

output :

1 2 3
1 2 3 : 4 5 6
 
matja, your an absolute star! been struggling with that for ages. now i can enjoy my weekend without a worry :-)
 
Back
Top Bottom