HCS12 - Const Array of Struct-pointers

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

HCS12 - Const Array of Struct-pointers

Jump to solution
1,981 Views
sebasira
Senior Contributor I

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!

Labels (1)
0 Kudos
Reply
1 Solution
786 Views
CompilerGuru
NXP Employee
NXP Employee

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

 

View solution in original post

0 Kudos
Reply
4 Replies
787 Views
CompilerGuru
NXP Employee
NXP Employee

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

 

0 Kudos
Reply
786 Views
crane
Contributor III

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

0 Kudos
Reply
786 Views
sebasira
Senior Contributor I

Thanks for that tip!! It's very useful!

 

 

0 Kudos
Reply
786 Views
sebasira
Senior Contributor I

Thank you very much!!!!!

0 Kudos
Reply