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

查看:71
本文介绍了何时/为什么需要 '\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?

如果我创建了一个字符数组:

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.

如果要确定字符串存储在字符数组中的长度,可以使用标头 中声明的标准 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天全站免登陆