cancel
Showing results for 
Search instead for 
Did you mean: 

Definition of a array of char[] fails, if its size is > 8

TheaArt
Associate III

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.

1 ACCEPTED SOLUTION

Accepted Solutions
AScha.3
Chief III

missing line end comma , lines 24 + 25 ; so index end is 8.

0693W00000WImdPQAT.png+

When your question is answered, please close this topic by choosing Select as Best.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

9 REPLIES 9
gbm
Lead III

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.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
TheaArt
Associate III

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?

AScha.3
Chief III

missing line end comma , lines 24 + 25 ; so index end is 8.

0693W00000WImdPQAT.png+

When your question is answered, please close this topic by choosing Select as Best.

If you feel a post has answered your question, please click "Accept as Solution".
TheaArt
Associate III

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.

Bob S
Principal

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.

const char * const myvector[];

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
TheaArt
Associate III

Good morning,

thank you for the hints for constant definition.

Also from C99 a designated initializers with those individual enum values can be used.

https://en.cppreference.com/w/c/language/array_initialization

This looks nice, although it is not really intuitive, it's very self-explanatory.