Skip to main content
TheaArt
Associate II
Nov 18, 2022 7:28 AM UTC
Solved

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

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.

This topic has been closed for replies.
AScha.3
AScha.3Best answer
Super User
Nov 18, 2022 9:21 AM UTC

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 on " Best Answer ".

9 replies

gbm
Super User
Nov 18, 2022 8:18 AM UTC

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
TheaArtAuthor
Associate II
Nov 18, 2022 8:34 AM UTC

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?

gbm
Super User
Nov 18, 2022 5:53 PM UTC

const char * const myvector[];

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
AScha.3
AScha.3Best answer
Super User
Nov 18, 2022 9:21 AM UTC

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 on " Best Answer ".
TheaArt
TheaArtAuthor
Associate II
Nov 18, 2022 9:39 AM UTC

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
Super User
Nov 18, 2022 4:51 PM UTC

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.

Piranha
Principal III
Nov 22, 2022 2:45 AM UTC

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

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

TheaArt
TheaArtAuthor
Associate II
Nov 22, 2022 7:25 AM UTC

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

TheaArt
TheaArtAuthor
Associate II
Nov 21, 2022 7:00 AM UTC

Good morning,

thank you for the hints for constant definition.