C语言 如何以字节为单位获取字符串大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15000544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:24:27  来源:igfitidea点击:

How to get the string size in bytes?

csizeofarrays

提问by artaxerxe

As the title implies, my question is how to get the size of a string in C. Is it good to use sizeofif I've declared it (the string) in a function without mallocin it? Or, if I've declared it as a pointer? What if I initialized it with malloc? I would like to have an exhaustive response.

正如标题所暗示的,我的问题是如何在C. 它的好处是使用sizeof不,如果我已经声明它的功能(串)malloc的呢?或者,如果我已将其声明为指针?如果我用 初始化它malloc怎么办?我想得到详尽的答复。

回答by nmikhailov

You can use strlen. Size is determined by the terminating null-character, so passed string should be valid.

您可以使用strlen。大小由终止空字符决定,因此传递的字符串应该是有效的。

If you want to get size of memory buffer, that contains your string, and you have pointer to it:

如果您想获取包含您的字符串的内存缓冲区的大小,并且您有指向它的指针:

  • If it is dynamic array(created with malloc), it is impossible to get it size, since compiler doesn't know what pointer is pointing at. (check this)
  • If it is static array, you can use sizeofto get its size.
  • 如果它是动态数组(用 malloc 创建),就不可能得到它的大小,因为编译器不知道指针指向什么。(检查这个
  • 如果是静态数组,则可以使用sizeof获取其大小。

If you are confused about difference between dynamic and static arrays, check this.

如果您对动态数组和静态数组之间的区别感到困惑,请查看

回答by Bernhard Barker

Use strlento get the length of a null-terminated string.

使用strlen获得的长度空终止字符串

sizeofreturns the length of the arraynot the string. If it's a pointer (char *s), not an array (char s[]), it won't work, since it will return the size of the pointer (usually 4 bytes on 32-bit systems). I believe an array will be passed or returned as a pointer, so you'd lose the ability to use sizeofto check the size of the array.

sizeof返回数组的长度而不是字符串。如果它是一个指针 ( char *s),而不是一个数组 ( char s[]),它将不起作用,因为它将返回指针的大小(在 32 位系统上通常为 4 个字节)。我相信数组将作为指针传递或返回,因此您将失去sizeof用于检查数组大小的能力。

So, only if the string spans the entire array(e.g. char s[] = "stuff"), would using sizeoffor a statically definedarray return what you want (and be faster as it wouldn't need to loop through to find the null-terminator) (if the last character is a null-terminator, you will need to subtract 1). If it doesn't span the entire array, it won't return what you want.

因此,只有当整个阵列的字符串跨度(例如char s[] = "stuff"),将使用sizeof一个静态定义你想要什么回报阵列(且更快,因为它不会需要遍历找到空终止符)(如果最后一个字符是空终止符,您需要减去 1)。如果它不跨越整个数组,它不会返回你想要的。

An alternative to all this is actually storing the size of the string.

所有这些的替代方法实际上是存储字符串的大小。

回答by hyde

While sizeofworks for this specific type of string:

虽然sizeof适用于这种特定类型的字符串:

char str[] = "content";
int charcount = sizeof str - 1; // -1 to exclude terminating '
(strlen(string) + 1 ) * sizeof(char)
'

It does not work if stris pointer (sizeofreturns size of pointer, usually 4 or 8) or array with specified length (sizeofwill return the byte count matching specified length, which for char type are same).

如果str是指针(sizeof返回指针的大小,通常为 4 或 8)或具有指定长度的数组(sizeof将返回匹配指定长度的字节数,对于 char 类型相同),则不起作用。

Just use strlen().

只需使用strlen().

回答by Aniket Bhanja

If you use sizeof()then a char *strand char str[]will return different answers. char str[]will return the length of the string(including the string terminator) while char *strwill return the size of the pointer(differs as per compiler).

如果您使用sizeof()then a char *strandchar str[]将返回不同的答案。char str[]将返回字符串的长度(包括字符串终止符),同时char *str将返回指针的大小(因编译器而异)。

回答by A.Clymer

I like to use:

我喜欢使用:

const char* message = "%s, World!";
char* string = (char*)malloc((strlen(message)+1))*sizeof(char));
snprintf(string, (strlen(message)+1))*sizeof(char), message, "Hello");

This will give you the buffer size in bytes. You can use this with snprintf() may help:

这将为您提供以字节为单位的缓冲区大小。您可以将其与 snprintf() 一起使用可能会有所帮助:

##代码##

Cheers! Function: size_t strlen (const char *s)

干杯! 功能:size_t strlen (const char *s)