Jump to content

Is this considered a bad practice?

P1X3

If I remember correctly, my professor mentioned something about avoiding anonymous arrays. Problem is I don't remember if we were using Java or C++.

byte* EXTRAMSGS[] = {  (byte[]){ 0x3F, 0x05, 0x00, 0x0C, 0x52, 0x01 },          // OpenWindowFrontDriver	// Extra  (byte[]){ 0x3F, 0x06, 0x00, 0x0C, 0x01, 0x31, 0x01 },    // FoldInDriverMirror  (byte[]){ 0x3F, 0x06, 0x00, 0x0C, 0x01, 0x30, 0x01 },    // FoldOutDriverMirror};void sendBusMessage(byte data[]){  byte checksum = 0x00;  byte length = (sizeof(data)/sizeof(byte));	  for (int i =0; i < length; i++)    checksum ^= data[i];		  KBUS.write(data, length);  KBUS.write(checksum);}int msgId = (data & 0b00111111);sendBusMessage(EXTRAMSGS[msgId]);

Edit: Sorry, I ended up cutting out the rest of the post by accident. What I am trying to accomplish is array of pointers to byte arrays. I considered 2D array, but byte arrays aren't of equal length.

The above posted code, I haven't actually tested it, but I think it should work, in theory... I haven't touched anything C/C++ related in over a year. Java makes me lazy :\

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

From what I can see, my personal opinion is that you are all good.

 

As a note, I am not really sure that really is an anonymous array...as you can easily re-access the arrays *as they are part of the other array*

 

A more classical example of an anonymous array, which I would say would be a bad practice would be this

sendBusMessage((byte[]){ 0x3F, 0x05, 0x00, 0x0C, 0x52, 0x01 });

But given that you have set the arrays to a variable, they really aren't classified as anonymous and I would say it is good....the main reason against anonymous arrays would likely be that they are not really readable and not as easy to change *if you are working with a large program*

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

byte length = (sizeof(data)/sizeof(byte));

this won't work: sizeof(data) will equal sizeof(byte*) because the array size information gets lost when you pass through the pointer array

arrays store the number of elements, pointers don't

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×