Although I have read several pages about C pointer syntax I remain confused... let's say I have two char arrays (looking up UI button ids and mapping them into bytes for output). There are 15 buttons on page 1 of the UI and 20 buttons on page 2, so...
char keyMap1[15] = {
'i', // Ign
'z', // TA Zoom 'z'
'b', // Brake 'b'
KEY_UP_ARROW, // Shift Up KEY_UP_ARROW
0x20, // ESC
// end row 1
'l', // Lights 'l'
'x', // TA page 'x'
'1', // Cab View '1'
...
char keyMap2[20] = {
'c', // Signal Left 'c'
0x20, // BLANK?
'x', // TA Page 'x'
'z', // TA Zoom
'v', // Signal Right 'v'
// end row 1
'i', // Ign
't', // Trailer 't'
...
Later in the code when someone hits a button on a certain UI page with a certain ID, I could do this:
byte c;
if (pg == 1) {
c = keyMap1[i];
} else {
c = keyMap2[i];
}
And this works. But I thought it would be more elegant if I set up a 2-element array containing the addresses of the two keyMap char arrays, something like this:
char *keyMaps[2] = {
&keyMap1,
&keyMap2,
};
and then later do this
char *cp[] = keyMaps[pg-1];
c = cp[i];
it doesn't really make much difference either way... but I find myself among the many who get flustered by the special syntax for pointers. I thought I had this right, but I get compile time errors. Doubtless if you really know your c syntax, I did something ludicrous; but I was trying to apply what I had been reading.
I don't mind doing this the dumb way
whatever works! just wondering why my attempt to be clever failed. I thought this kind of trick (stashing pointers in arrays) could potentially be a more elegant way to implement a dispatch table than long switch statements, too.