If you have to initialize a pointer for the literal, you can use the following:
const char un_str[] = "...", *un_ptr = un_str;
const size_t un_len = sizeof(un_str);
Note: sizeof()
returns a size_t, which is unsigned.
You can pack that into a macro:
#define STR_LEN_PAIR(name, value) const char name##_str[] = value; \
const size_t name##_len = sizeof(name##_str)
This uses argument concatenation. Note the missing ;
after the last line which allows to use standard syntax on invocation. Be careful to use it only as intended:
// at file level
STR_LEN_PAIR(un,"...");
The fields can be accessed like un_len
, un_str
, ...
Note that the pointer itself is not const, so there is not much use in it (you quite likely do not want to have a modifyable pointer to each such string). You can simply omit its definitions as I already did in the *macro.
Compatibility note:
C differs in the semantics of const
. Actually, C does not have "constants" as C++ has. objects defined as const
are actually "unmodifyable variables": they can only be initialized at compiler-time, but they consume space as any other variable (however, they might be located in unchangeable memory) and the compiler will generate read accesses as for any other variable. In C++, un_len
for instance will be replaced by its value at compile-time and might consume no memory in the final program - as long as its address is not taken.
What comes next to real constants in C are either enum
constants, or `#define'ed names. The latter are processed at a very different level (textual replacement by the pre-processor), however.