Skip to main content
TheaArt
Associate II
November 18, 2022
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
November 18, 2022

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
November 18, 2022

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
November 18, 2022

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
November 18, 2022

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
November 18, 2022

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
November 18, 2022

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
November 18, 2022

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
November 22, 2022

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
November 22, 2022

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

TheaArt
TheaArtAuthor
Associate II
November 21, 2022

Good morning,

thank you for the hints for constant definition.