字符串数组的初始化字符串太长C [英] initializer-string for array of chars is too long C

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

问题描述

我正在编写一个接受输入的程序,并输出与输入相对应的数值。我得到char部分的错误。我不明白为什么它会有这样的错误,当数组中只有27个字符的大小为27?

I'm working on a program that accepts input and and outputs a numerical value corresponding to the input. I get the error on the char part. I don't understand why it would have an error like that when there's only 27 characters in the array that has a size of 27?

int main ()
{
    char greek[27] = "ABGDE#ZYHIKLMNXOPQRSTUFC$W3";
}


推荐答案

c $ c> [28] 将尾随'\0'设置为有效的字符串。

You need one more [28] for the trailing '\0' to be a valid string.

查看 C编程注释:第8章:字符串


C中的字符串由字符数组表示。
字符串的结尾标有一个特殊字符,空字符,
只是一个值为0的字符。(空字符没有
关系,除了名字中的空指针,在ASCII字符
中,空字符命名为NUL。)空或字符串终止
字符由另一个字符转义序列\0表示。

Strings in C are represented by arrays of characters. The end of the string is marked with a special character, the null character, which is simply the character with the value 0. (The null character has no relation except in name to the null pointer. In the ASCII character set, the null character is named NUL.) The null or string-terminating character is represented by another character escape sequence, \0.

正如Jim Balter和Jayesh所指出的,当你提供初始值时,你可以省略数组大小(编译器使用初始化器的数量作为数组

And as pointed out by Jim Balter and Jayesh, when you provide initial values, you can omit the array size (the compiler uses the number of initializers as the array size).

char greek[] = "ABGDE#ZYHIKLMNXOPQRSTUFC$W3";

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

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