union _Test
	{
		unsigned char keydata[4];
		struct _Member
		{
			unsigned char a;
			unsigned char b;
			unsigned char c;
			unsigned char d;
		}Member;
	}Test;
Test.keydata[4]= {
		1,
		2,
		3,
		4};

error on line 12: expected expression before '{' token.

If I declare without union,

const unsigned char keydata[NUM_KEYS]={
	2,
	1,
	6,
	5,
	4,
	7,
	8,
	9,
	14,	// DOWN
	10,	// ON/OFF
	13,	// RIGHT
	11,	// UP
	12, // LEFT
	16,	// PUT
	15,	// PLACE
	3
};

This work and compile success

If I declare without array,

union DisplayData
{
	struct data2bytes
	{
		unsigned char lbyte;
		unsigned char hbyte;
	};
	
	unsigned int data;
};

This also work and compile success

Anyone can guide me through this error?

Edited 5 Years Ago by murnesty: n/a

Test.keydata[4]= {
1,
2,
3,
4};

This is a list-initializer used for initialization.
You should...

int main()
{
    for(int i = 0; i < 4; ++i)
        Test.keydata[i] = i + 1;
}

Q.
Your char array is 4-chars long. What are the elements it produces/contains?
A.
It produces/contains elements keydata[0] through keydata[3].

Q.
Have another look at Line 18. What element are you attempting to access?
A.
You are attempting to access element 4, which does not exist because it is the fifth (5th) element.

Also, as jinhao correctly points out, you are attempting to use an initialization list in an illegal manner.

Edited 5 Years Ago by Fbody: n/a

jinhao.. Yes I just realize it is a for initialization only. I don't want to use for loop to assign. Because actually the array size is not only 4.. The 1 I post just sample. Actual one have around 1000+-. If use loop will like wasting time.

Or, In real system. Initialize

Test.keydata[4]= {
1,
2,
3,
4};

will used same amount of time compare to

int main()
{
    for(int i = 0; i < 4; ++i)
        Test.keydata[i] = i + 1;
}

?

By the time you use Test.keydata it has already been initialized. If you want Test to take values on initialization you could do something like

union tTest {
    unsigned char keydata[4];
    struct tMember {
        unsigned char a;
        unsigned char b;
        unsigned char c;
        unsigned char d;
    } Member;
} Test = {{ 1,2,3,4 }};
This article has been dead for over six months. Start a new discussion instead.