简体   繁体   English

C中的sizeof(char [])

[英]Sizeof(char[]) in C

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[]的大小总是加一?

Note the difference between sizeof and strlen . 注意sizeofstrlen之间的区别。 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. 第二个函数返回字符串的长度,该函数将小于其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。

As Michael pointed out in the comments the strings are terminated by a zero. 正如Michael在评论中指出的那样,字符串以零结尾。 So in memory the first string will look like this 所以在内存中第一个字符串看起来像这样

"123\0"

where \\0 is 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? 如果你没有这个终止字符,那么怎么知道字符串(或者char[]的结尾)? Well, indeed one other way is to store the length somewhere. 嗯,确实另一种方法是在某处存储长度。 Some languages do that. 有些语言可以做到。 C doesn't. C没有。

In C, strings are stored as arrays of char s. 在C中,字符串存储为char的数组。 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. 使用识别的终止字符( '\\0'或只是0 ),您可以传递指向字符串的指针,而不需要任何其他元数据。 When processing a string, you read chars from the memory pointed at by the pointer until you hit the terminating value. 处理字符串时,从指针指向的内存中读取字符,直到达到终止值。

As your array initialisation is using a string literal: 由于您的数组初始化使用字符串文字:

char name[]="123";

is equivalent to: 相当于:

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

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[]={'1','2','3'};

or 要么

char name[3]="123";

(thanks alk) which will do as you were expecting. (感谢alk)会像你期待的那样做。

Because there is a null character that is attached to the end of string in C. 因为在C中的字符串末尾附加了一个空字符。

Like here in your case 就像你的情况一样

name[0] = '1'
name[1] = '2'
name[2] = '3'
name[3] = '\0'

name1[0] = '1'
name1[1] = '2'
name1[2] = '3'
name1[3] = '4'
name1[4] = '\0'
name = {'1','2','3','\0'};
name1 = {'1','2','3','4','\0'};

So 所以

sizeof(name) = 4;
sizeof(name1) = 5;

sizeof returns 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字节。

In C, string literals have a null terminating character added to them. 在C中,字符串文字添加了空终止字符。

Your strings, 你的弦乐,

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

look more like: 看起来更像:

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

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。

A String in C (and in, probably, every programming language - behind the scenes) is an array of characters which is terminated by \\0 with the ASCII value of 0. C中的String (可能是在每种编程语言中,在幕后)是一个字符数组,由\\0以ASCII值0结束。

When assigning: char arr[] = "1234"; 分配时: char arr[] = "1234"; , you assign a string literal, which is, by default, null-terminated ( \\0 is also called null) as you can see here . ,你可以在这里看到一个字符串文字,默认情况下是空终止的( \\0也叫做null)。

To avoid a null (assuming you want just an array of char s and not a string), you can declare it the following way char arr[] = {'1', '2', '3', '4'}; 为了避免null(假设你只需要一个char数组而不是一个字符串),你可以用以下方式声明它: char arr[] = {'1', '2', '3', '4'}; and the program will behave as you wish ( sizeof(arr) would be 4). 并且程序将按照您的意愿运行( sizeof(arr)将为4)。

每个字符串都以char nullbyte'\\ 0'结束,它将长度加1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM