0

I need to fill a byte array in c language by the possible enumerations.

Here it is how I declare my byte array:

unsigned char byteArray[6];

And now I hesitate if my enumrations should look like MyEnum1 or MyEnum2:

enum MyEnum1 { A1 = 0, B1 = 1, C1 = 2};
enum MyEnum2 { A2 = 0x00, B2 = 0x01, C2 = 0x02}; 

The purpose is something like this:

byteArray[0]=A1;
byteArray[1]=B1;
byteArray[2]=C1;
byteArray[3]=A2;
byteArray[4]=B2;
byteArray[5]=C2;

So, is there any recommandations about if my enumerations should contain hexadecimal or integers or another type of data??

Thank you!

Farah
  • 2,469
  • 5
  • 31
  • 52

2 Answers2

2

In your case no matter how you will organize your enum. You can do it in both ways and the result will be the same.

Vladyslav
  • 786
  • 4
  • 19
1

It doesn't matter. Your two enumerations:

enum MyEnum1 { A1 = 0, B1 = 1, C1 = 2};
enum MyEnum2 { A2 = 0x00, B2 = 0x01, C2 = 0x02}; 

are strictly equivalent, just as int i = 0x5 is strictly quivalent to int i = 5. It's up to you if you want to use decimal or hexadecimal representations of your constants.

And BTW instead of:

unsigned char byteArray[6];

you should probably have:

enum MyEnum1 byteArray[6];
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115