2022-11-17 11:28 PM
Hello community,
I use CubeIde to program my STM32 L051R6T6.
Language is C.
I do the following definition in a .c file:
enum internalFaults{
internalFaultsTransmitPrepare,
internalFaultsTransmit,
internalFaultsWatchdogAlarm,
internalFaultsTelegramsRxSequence,
internalFaultsNoTelegramsReceived,
internalFaultsTelegramsNotEqual,
internalFaultsTelegramProcessing,
internalFaultsMaxim5320EAI,
internalFaultsUARTRead,
internalFaultsUARTStartRxInterrupt,
internalFaultsCount,
};
const char* internalFaultNames[internalFaultsCount] =
{
"TransmitPrepareFault",
"TransmitFault",
"WatchdogExeededFault",
"TelegramsRxSequenceFault",
"NoTelegramsReceivedFault",
"TelegramsNotEqualFault",
"TelegramProcessingFault",
"Maxim5320EAIFault"
"UARTReadFault"
"UARTStartRxInterrupt"
};
There is no write access to internalFaultNames or to its char[]'s.
But when I read from it
strcat(message,internalFaultNames[faultIndex]);
It only works, if faultIndex is 0 <=faultIndex<8.
internalFaultNames[internalFaultsUARTRead] and internalFaultNames[internalFaultsUARTStartRxInterrupt]
are NULL.
This I can see in the debugger too.
Is this a special restriction of STM32 MCU or the Compiler user in CubeIDE?
Thank you.
Solved! Go to Solution.
2022-11-18 01:21 AM
missing line end comma , lines 24 + 25 ; so index end is 8.
+
When your question is answered, please close this topic by choosing Select as Best.
2022-11-18 12:18 AM
The behavior you observe is correct: you have declared a VARIABLE vector of char pointers to constant strings. The strings cannot be modified, the pointers can. The phenomenon with dependency on the value of index should not happen and I believe it doesn't happen - there might be another error in your program manifesting in overwritng one of the pointers in the vector - remember that they are variable with the current declaration.
2022-11-18 12:34 AM
Hello and thank you for reply.
Yes, I know, that the ponters in the array are variable. But nothing writes to it, as far as I can see.
I statrted the programm in debugger and it stops at the first line of main(). Nothing is done but only the definitions in the .c files.
And if i look to the expression internalFaultNames, the last two pointers are NULL.
How can I declare the array to const? Is there away to do so?
2022-11-18 01:21 AM
missing line end comma , lines 24 + 25 ; so index end is 8.
+
When your question is answered, please close this topic by choosing Select as Best.
2022-11-18 01:39 AM
OMG, what a stupid mistake. I was also wondering, why the 8th string was not terminated and continues with the text of 9th and 10th.
Sometimes, I'm a little bit blind.
Thank you, and sorry for that question.
2022-11-18 08:51 AM
static_assert() is your friend here.
#include <assert.h>
const char* internalFaultNames[internalFaultsCount] =
{
"TransmitPrepareFault",
"TransmitFault",
"WatchdogExeededFault",
"TelegramsRxSequenceFault",
"NoTelegramsReceivedFault",
"TelegramsNotEqualFault",
"TelegramProcessingFault",
"Maxim5320EAIFault"
"UARTReadFault"
"UARTStartRxInterrupt"
};
static_assert( sizeof(internalFaultNames)/sizeof(internalFaultNames[0]) == internalFaultsCount, "ERROR: Add/remove items" );
That will cause a compile-time error should you either leave off a comma, or add an enum value without adding the string.
When I am feeling REALLY paranoid, I even validate the individual enum values, just in case I (or someone else) insert a new value in the middle somewhere.
2022-11-18 09:53 AM
const char * const myvector[];
2022-11-20 11:00 PM
Good morning,
thank you for the hints for constant definition.
2022-11-21 06:45 PM
Also from C99 a designated initializers with those individual enum values can be used.
https://en.cppreference.com/w/c/language/array_initialization
2022-11-21 11:25 PM
This looks nice, although it is not really intuitive, it's very self-explanatory.