什么时候/为什么需要'\ 0'来标记(字符)数组的结尾? [英] When/Why is '\0' necessary to mark end of an (char) array?

查看:370
本文介绍了什么时候/为什么需要'\ 0'来标记(字符)数组的结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我仅阅读了一个有关如何创建代表字符串的字符数组的示例.

So I just read an example of how to create an array of characters which represent a string.

空字符\0放在数组的末尾以标记数组的末尾.这有必要吗?

The null-character \0 is put at the end of the array to mark the end of the array. Is this necessary?

如果我创建了一个char数组:

If I created a char array:

char line[100]; 

并输入:

"hello\n"

其中的字符将被放置在前六个索引line[0]-line[6]上,因此数组的其余部分仍然会填充空字符吗?

in it, the chars would be placed at the first six indexes line[0] - line[6], so the rest of the array would be filled with null characters anyway?

这本书说,这是一个惯例,例如,将字符串常量"hello\n"放在字符数组中,并以\0终止.

This books says, that it is a convention that, for example the string constant "hello\n" is put in a character array and terminated with \0.

也许我不完全理解这个主题,希望能对我有所启发.

Maybe I don't understand this topic to its full extent and would be glad for enlightenment.

推荐答案

何时/为什么需要'\ 0'标记(字符)数组的结尾?

When/Why is '\0' necessary to mark end of an (char) array?

如果字符数组包含字符串,则必须以零结尾.这样可以找到字符串结束的点.

The terminating zero is necessary if a character array contains a string. This allows to find the point where a string ends.

对于您的示例,我认为它的显示方式如下

As for your example that as I think looks the following way

char line[100] = "hello\n";

然后对于初学者,字符串文字包含7个字符.它是一个字符串,包括结尾的零.此字符串文字的类型为char[7].你可以想象它像

then for starters the string literal has 7 characters. It is a string and includes the terminating zero. This string literal has type char[7]. You can imagine it like

char no_name[] = { 'h', 'e', 'l', 'l', 'o', '\n', '\0' };

当字符串文字用于初始化字符数组时,其所有字符均用作初始化程序.因此,相对于示例,字符串文字的七个字符用于初始化数组的前7个元素.数组中所有其他未由字符串文字的字符初始化的元素都将由零隐式初始化.

When a string literal is used to initialize a character array then all its characters are used as initializers. So relative to the example the seven characters of the string literal are used to initialize first 7 elements of the array. All other elements of the array that were not initialized by the characters of the string literal will be initialized implicitly by zeroes.

如果要确定字符串存储在字符数组中的时间,可以使用标头<string.h>中声明的标准C函数strlen.它返回数组中终止于零之前的字符数.

If you want to determine how long is the string stored in a character array you can use the standard C function strlen declared in the header <string.h>. It returns the number of character in an array before the terminating zero.

考虑以下示例

#include <stdio.h>
#include <string.h>

int main(void) 
{
    char line[100] = "hello\n";

    printf( "The size of the array is %zu"
            "\nand the length of the stored string \n%s is %zu\n",
            sizeof( line ), line, strlen( line ) );

    return 0;
}

其输出为

The size of the array is 100
and the length of the stored string 
hello
 is 6

在C语言中,您可以使用字符串文字来初始化字符数组,但不包括字符串文字的结尾零.例如

In C you may use a string literal to initialize a character array excluding the terminating zero of the string literal. For example

char line[6] = "hello\n";

在这种情况下,您可能不会说数组包含一个字符串,因为存储在数组中的符号序列不具有结尾的零.

In this case you may not say that the array contains a string because the sequence of symbols stored in the array does not have the terminating zero.

这篇关于什么时候/为什么需要'\ 0'来标记(字符)数组的结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆