I'm looking to find the length of a C-string literal at compile time. Given the definitions:
static const char * const header_left[] =
{
" | | Raw | Raw |",
" | | Start | End |",
"Interval#| Duration | Point | Point |",
"---------+----------+-------+-------+",
};
const unsigned int rows_in_header = sizeof(header_left) / sizeof(header_left[0]);
How do I find the length of string literal header_left[2]
without using strlen
?
In this question, Determining the Length of a String Literal, there is a comment to declare the array as header_left[][4]
. I prefer not to use this kind of declaration as there is a tendency for the number of strings to change without changing the quantity constant. I like having the compiler calculate the quantity of strings (see the rows_in_header
definition) and the length of each string.
This is for an embedded system and the strings are block written to a serial port. The serial port function takes a pointer to the data and the length of the data as parameters. The serial port code is optimized for block writes. The preference is not to use strlen
because that wastes performance time.
I am using C99 with IAR Embedded Workshop on an ARM7TDMI platform.
I have included the c++
tag because this also involves C++ and we will be migrating the code to C++ after first product launch.
static const char * const header_left[]
the length is irrelevant, since that's an array of pointers. Did you perhaps wantstatic const char header_left[][sizeof "Interval#| Duration | Point | Point |"] = { ... };
? – Daniel Fischersizeof
and other time between the{
and}
? – Thomas Matthews