字符数组初始化困境 [英] Char array initialization dilemma

查看:24
本文介绍了字符数组初始化困境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

//hacky,因为123"是 4 个字符长(包括终止 0)字符符号[3] = "123";//干净,但打字很多字符符号[3] = {'1', '2', '3'};

所以,twist 实际上是在代码的注释中描述的,有没有办法用字符串文字初始化 char[] 而不终止零?

更新:似乎 IntelliSense 确实是错误的,此行为在 C 标准中明确定义.

解决方案

这个

char符号[3] = "123";

是一个有效的声明.

根据 1988 年的 ANSI C 规范:

<块引用>

字符类型的数组可以用字符串初始化文字,可选地括在大括号中.的连续字符字符串文字(包括终止空字符,如果有空间或数组大小未知)初始化数组的成员.

因此,您所做的在技术上是没问题的.

请注意,字符数组是对初始值设定项的规定约束的一个例外:

<块引用>

初始化器列表中不应有更多的初始化器是要初始化的对象.

然而,一段代码的技术正确性只是该代码优点"的一小部分.行 char symbols[3] = "123"; 将立即让资深程序员怀疑,因为从表面上看,它似乎是一个有效的字符串初始化,以后可以这样使用,导致到意外错误和必死无疑.

如果你想走这条路,你应该确定这是你真正想要的.保存那个额外的字节不值得你陷入这样的麻烦.NULL 符号(如果有的话)允许您编写更好、更灵活的代码,因为它提供了一种明确的(在大多数情况下)终止数组的方式.

(可在此处获得规范草案.)

为了补充本页其他地方的 Rudy 评论,C99 草案规范的第 32 个示例在 §6.7.8(第 130 页)中指出这些行

char s[] = "abc", t[3] = "abc";

相同

char s[] = { 'a', 'b', 'c', '\0' },t[] = { 'a', 'b', 'c' };

从中您可以推断出您正在寻找的答案.

可以在此处找到 C99 规范草案.>

Consider following code:

// hacky, since "123" is 4 chars long (including terminating 0)
char symbols[3] = "123";

// clean, but lot of typing
char symbols[3] = {'1', '2', '3'};

so, the twist is actually described in comment to the code, is there a way to initialize char[] with string literal without terminating zero?

Update: seems like IntelliSense is wrong indeed, this behaviour is explicitly defined in C standard.

解决方案

This

char symbols[3] = "123";

is a valid statement.

According to the ANSI C Specification of 1988:

An array of character type may be initialized by a character string literal, optionally enclosed in braces. Successive characters of the character string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the members of the array.

Therefore, what you're doing is technically fine.

Note that character arrays are an exception to the stated constraints on initializers:

There shall be no more initializers in an initializer list than there are objects to be initialized.

However, the technical correctness of a piece of code is only a small part of that code's "goodness". The line char symbols[3] = "123"; will immediately strike the veteran programmer as suspect because it appears, at face value, to be a valid string initialization and later may be used as such, leading to unexpected errors and certain death.

If you wish to go this route you should be sure it's what you really want. Saving that extra byte is not worth the trouble this could get you into. The NULL symbol, if anything, allows you to write better, more flexible code because it provides an unambiguous (in most instances) way of terminating the array.

(Draft specification available here.)

To co-opt Rudy's comment elsewhere on this page, the C99 Draft Specification's 32nd Example in §6.7.8 (p. 130) states that the lines

char s[] = "abc", t[3] = "abc";

are identical to

char s[] = { 'a', 'b', 'c', '\0' },
t[] = { 'a', 'b', 'c' };

From which you can deduce the answer you're looking for.

The C99 specification draft can be found here.

这篇关于字符数组初始化困境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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