Determining the Length of a String Literal

耗尽温柔 提交于 2019-12-18 18:51:57

问题


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.


回答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.




回答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!




回答3:


strlen maybe?

size_t size = strlen(textMessages[2]);



回答4:


You should use the strlen() library method to get the length of a string. sizeof will give you the size of textMessages[2], a pointer, which would be machine dependent (4 bytes or 8 bytes).




回答5:


strlen gives you the length of string whereas sizeof will return the size of the Data Type in Bytes you have entered as parameter.

strlen

sizeof




回答6:


You could exploit the fact, that values in an array are consecutive:

const char *messages[] = {
    "footer",
    "barter",
    "banger"
};

size_t sizeOfMessage1 = (messages[1] - messages[0]) / sizeof(char); // 7   (6 chars + '\0')

The size is determined by using the boundaries of the elements. The space between the beginning of the first and beginning of the second element is the size of the first.

This includes the terminating \0. The solution, of course, does only work properly with constant strings. If the strings would've been pointers, you would get the size of a pointer instead the length of the string.

This is not guaranteed to work. If the fields are aligned, this may yield wrong sizes and there may be other caveats introduced by the compiler, like merging identical strings. Also you'll need at least two elements in your array.




回答7:


I'll tell you something, as per my Knowledge arrays an pointers are the same thing except when you use sizeof.

When you use sizeof on a pointer it will return always 4 BYTE regardless the thing that pointer points to, but if it used on array it will return How long the array is big in bytes?.

In you example here *textMessage[] is array of pointer so when you use sizeof(textMessage[2]) it will return 4 BYTE because textMessage[2] is a pointer.

I hope it'll be useful for you.



来源:https://stackoverflow.com/questions/12541739/determining-the-length-of-a-string-literal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!