Determining the Length of a String Literal
Solution 1
If you want the number computed at compile time (as opposed to at runtime with strlen
) it is perfectly okay to use an expression like
sizeof "A really large text message that "
"is spread over multiple lines";
You might want to use a macro to avoid repeating the long literal, though:
#define LONGLITERAL "A really large text message that " \
"is spread over multiple lines"
Note that the value returned by sizeof
includes the terminating NUL, so is one more than strlen
.
Solution 2
My suggestion would be to use strlen and turn on compiler optimizations.
For example, with gcc 4.7 on x86:
#include <string.h>
static const char *textMessages[3] = {
"Small text message",
"Slightly larger text message",
"A really large text message that "
"is spread over multiple lines"
};
size_t longmessagelen(void)
{
return strlen(textMessages[2]);
}
After running make CFLAGS="-ggdb -O3" example.o
:
$ gdb example.o
(gdb) disassemble longmessagelen
0x00000000 <+0>: mov $0x3e,%eax
0x00000005 <+5>: ret
I.e. the compiler has replaced the call to strlen
with the constant value 0x3e = 62.
Don't waste time performing optimizations that the compiler can do for you!
Solution 3
strlen gives you the length of string whereas sizeof will return the size of the Data Type in Bytes you have entered as parameter.
Related videos on Youtube
Zack
Updated on July 11, 2022Comments
-
Zack almost 2 years
Given an array of pointers to string literals:
char *textMessages[] = { "Small text message", "Slightly larger text message", "A really large text message that " "is spread over multiple lines" }
How does one determine the length of a particular string literal - say the third one? I have tried using the sizeof command as follows:
int size = sizeof(textMessages[2]);
But the result seems to be the number of pointers in the array, rather than the length of the string literal.
-
Ben Cottrell over 11 years
sizeof(textMessages[2])
will yield the size of thechar*
type. It's purely coincidental thatsizeof(char*)
happens to be 4 on your system, and nothing to do with the number of elements in your array. -
Jens over 11 years@MichaelFoukarakis There are only three pointers in the array (note the string concatenation).
-
-
Jens over 11 yearsWhy compute this number at runtime when it can be determined at compile time?
-
Tudor over 11 years@Jens: Not unless you also know the string at compile time.
-
Jens over 11 yearsThis is not guaranteed to work. While the pointers in the array must be consecutive, the string literals need not. Think of alignment and such. Or using the same pointer for identical string literals.
-
Mahmoud Emam over 11 yearsI didn't understand what you want
-
Jens over 11 yearsMy intent was to tell you to improve your knowledge on the difference of arrays and pointers by reading all of c-faq.com/aryptr/index.html Arrays are not pointers and they are often 2 or 8 instead of 4 bytes and their size may even depend on what type they point to. So, most of what you wrote is technically incorrect.
-
nemo over 11 yearsYou're right, alignment would break this (but that's controllable). Different sizes for
char
would break this, too. I'll mark my answer as not reliable. -
Jens over 11 yearsMerging identical strings, a common optimization, also breaks it.
-
Zack over 11 yearsI wish to determine the length of each string so that I can index the lengths and pass them to a function. Is it possible to compute such an array of lengths at compile time?
-
Jens over 11 yearsMy approach would be to declare and initialize an array of
struct { char *ptr; size_t len; } x[]
. Then you can callfoo(x[i].ptr, x[i].len)
. Since C doesn't keep string lengths around, there is no other way than to keep track of string lengths in your code. -
ideasman42 over 11 years@Tudor, yes - you do know the length at compile time (the question says "String Literal")
-
Jeff G over 8 yearsPointers are not always 4 bytes.