C语言 C 中的 sizeof(char[])

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30101872/
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 11:55:38  来源:igfitidea点击:

Sizeof(char[]) in C

carrayssizeof

提问by TobiMcNamobi

Consider this code:

考虑这个代码:

 char name[]="123";
 char name1[]="1234";

And this result

而这个结果

The size of name (char[]):4
The size of name1 (char[]):5

Why the size of char[]is always plus one?

为什么大小char[]总是加一?

回答by Weather Vane

Note the difference between sizeofand strlen. The first is an operator that gives the size of the whole data item. The second is a function that returns the length of the string, which will be less than its sizeof(unless you've managed to get string overflow), depending how much of its allocated space is actually used.

注意之间的差异sizeofstrlen。第一个是给出整个数据项大小的运算符。第二个是返回字符串长度的函数,该长度将小于它的长度sizeof(除非您设法使字符串溢出),这取决于实际使用了多少分配的空间。

In your example

在你的例子中

char name[]="123";

sizeof(name)is 4, because of the terminating '\0', and strlen(name)is 3.

sizeof(name)是 4,因为终止'\0',并且strlen(name)是 3。

But in this example:

但在这个例子中:

char str[20] = "abc";

sizeof(str)is 20, and strlen(str)is 3.

sizeof(str)是 20,strlen(str)是 3。

回答by TobiMcNamobi

As Michael pointed out in the comments the strings are terminated by a zero. So in memory the first string will look like this

正如迈克尔在评论中指出的,字符串以零结尾。所以在内存中第一个字符串看起来像这样

"123
char name[]="123";
"

where \0is a single char and has the ASCII value 0. Then the above string has size 4.

其中\0是单个字符,ASCII 值为 0。那么上述字符串的大小为 4。

If you had not this terminating character, how would one know, where the string (or char[]for that matter) ends? Well, indeed one other way is to store the length somewhere. Some languages do that. C doesn't.

如果你没有这个终止字符,你怎么知道字符串(或char[]就此而言)在哪里结束?嗯,确实另一种方法是将长度存储在某处。有些语言就是这样做的。C没有。

回答by Baldrickk

In C, strings are stored as arrays of chars. With a recognised terminating character ('\0'or just 0) you can pass a pointer to the string, with no need for any further meta-data. When processing a string, you read chars from the memory pointed at by the pointer until you hit the terminating value.

在 C 中,字符串存储为chars 的数组。使用可识别的终止字符('\0'或仅0),您可以传递指向字符串的指针,而无需任何进一步的元数据。处理字符串时,您从指针指向的内存中读取字符,直到遇到终止值。

As your array initialisation is using a string literal:

由于您的数组初始化使用字符串文字:

char name[]={'1','2','3',0};

is equivalent to:

相当于:

char name[]={'1','2','3'};

If you want your array to be of size 3 (without the terminating character as you are not storing a string, you will want to use:

如果您希望数组的大小为 3(没有终止字符,因为您不存储字符串,您将需要使用:

char name[3]="123";

or

或者

name[0] = '1'
name[1] = '2'
name[2] = '3'
name[3] = '
name = {'1','2','3','
sizeof(name) = 4;
sizeof(name1) = 5;
'}; name1 = {'1','2','3','4','
 char name[]="123";
 char name1[]="1234";
'};
' name1[0] = '1' name1[1] = '2' name1[2] = '3' name1[3] = '4' name1[4] = '
 char name[]="123##代码##";
 char name1[]="1234##代码##";
'

(thanks alk) which will do as you were expecting.

(感谢 alk)这会如你所愿。

回答by Zach P

A Stringin C (and in, probably, every programming language - behind the scenes) is an array of characters which is terminated by \0with the ASCII value of 0.

StringC 中的A (以及可能在每种编程语言中 - 在幕后)是一个字符数组,\0以 ASCII 值 0结尾。

When assigning: char arr[] = "1234";, you assign a string literal, which is, by default, null-terminated (\0is also called null) as you can see here.

分配时:char arr[] = "1234";,您分配一个字符串文字,默认情况下,它以空字符结尾(\0也称为空值),如您在此处所见。

To avoid a null (assuming you want just an array of chars and not a string), you can declare it the following way char arr[] = {'1', '2', '3', '4'};and the program will behave as you wish (sizeof(arr)would be 4).

为了避免空值(假设您只需要一个chars数组而不是字符串),您可以按以下方式声明它char arr[] = {'1', '2', '3', '4'};,程序将按您的意愿运行(sizeof(arr)将是 4)。

回答by Benison Sam

Because there is a null character that is attached to the end of string in C.

因为在 C 中字符串的末尾附加了一个空字符。

Like here in your case

就像你的情况一样

##代码##

回答by Gopi

##代码##

So

所以

##代码##

sizeofreturns the size of the object and in this case the object is an array and it is defined that your array is 4 bytes long in first case and 5 bytes in second case.

sizeof返回对象的大小,在这种情况下,对象是一个数组,它被定义为第一种情况下的数组长度为 4 个字节,第二种情况下为 5 个字节。

回答by Evdzhan Mustafa

In C, string literals have a null terminating character added to them.

在 C 中,字符串文字添加了一个空终止字符。

Your strings,

你的弦,

##代码##

look more like:

看起来更像:

##代码##

Hence, the size is always plus one. Keep in mind when reading strings from files or from whatever source, the variable where you store your string, should always have extra space for the null terminating character.

因此,大小总是加一。请记住,从文件或任何来源读取字符串时,存储字符串的变量应始终为空终止字符留出额外的空间。

For example if you are expected to read string, whose maximum size is 100, your buffer variable, should have size of 101.

例如,如果您希望读取最大大小为 100 的字符串,则您的缓冲区变量的大小应为 101。

回答by Victor de Quidt

Every string is terminated with the char nullbyte '\0' which add 1 to your length.

每个字符串都以 char nullbyte '\0' 结尾,它为您的长度加 1。