Hello everybody!
I'm working with a "constant array of pointers to a struct". (Please excuse my english, I'm not sure if that's written allright).
Let's put it into code so you can understand me:
typedef struct{ byte Tx_F; // Tx Flag byte cmd; // Command Number }typeTxCMD; typeTxCMD Command_00={0,25}; typeTxCMD Command_01={0,32}; typeTxCMD Command_02={0,44}; const typeTxCMD* TxCommand_Table[]={ &Command_00, &Command_01, &Command_02 }
Then, I want to set the Tx Flag of a Command, but that command depends on the value of a variable, let's name it as "var".
So, I do this:
TxCommand_Table[var]->Tx_F = 1;
And when compiling, and error pops up: "C1830: Modifiable lvalue expected"
To avoid this I did a little "trick" and works fine, but I want to know why it doesn't work the other way.
Here's what I did:
typeTxCMD* temp; temp = TxCommand_Table[var]; temp->Tx_F = 1;
Please I hope you guys can give me a hand here.
Best Regards!
Solved! Go to Solution.
That's actually a pretty common mistake, the problem is that not the array is const, but instead it contains pointers to const elements.
To use a const array with pointers to non const structs move the const after the *:
typeTxCMD* const TxCommand_Table[...
Daniel
That's actually a pretty common mistake, the problem is that not the array is const, but instead it contains pointers to const elements.
To use a const array with pointers to non const structs move the const after the *:
typeTxCMD* const TxCommand_Table[...
Daniel
It all makes perfect sense once you realize that you should read from right to left:
TxCommand_Table (is a ) const * (to a) typeTxCMD
Håkan
Thanks for that tip!! It's very useful!
Thank you very much!!!!!