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

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

问题描述

所以我刚刚阅读了一个关于如何创建表示字符串的字符数组的示例.

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

将空字符 放在数组的末尾,以标记数组的末尾.这是必要的吗?

The null-character 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
"

在其中,字符将被放置在前六个索引 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 " 放入字符数组并以 结尾.

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

也许我没有完全理解这个话题,希望能得到启发.

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

推荐答案

何时/为什么需要 '' 来标记 (char) 数组的结尾?

When/Why is '' 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
";

那么对于初学者来说,字符串文字有 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', '
', '' };

当使用字符串字面量来初始化字符数组时,它的所有字符都将用作初始化器.因此,相对于示例,字符串文字的七个字符用于初始化数组的前 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
";

    printf( "The size of the array is %zu"
            "
and the length of the stored string 
%s is %zu
",
            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
";

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

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.

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

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