c - 它会在末尾放置 0 或\0 吗?

标签 c arrays pointers

在 char arr[8] 中,arr[3] 是什么,是否将其视为非字符串数组,因为它们是字符(初始化为零)或字符串数​​组。

char arr[8]={'a','b','c'}//what will be arr[3] and onwards \0 or 0.

最佳答案

当您声明一个具有显式大小的数组并对其进行初始化时,该数组中任何未显式初始化的成员都将设置为 0。

这在 C standard 的第 6.7.9p21 节中指定。 :

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

所以arr[3]arr[4]arr[5]arr[6] > 和 arr[7] 都将为 0。

另请注意,0 和 '\0' 在源代码中是同一值的两种不同表示形式。

关于c - 它会在末尾放置 0 或\0 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52995734/

相关文章:

c++ - 通过 emplace() 将对象指针插入到 map 的 map 中不起作用

C - malloc 和自动存储

c++ - C++ 中的常量指针变量

c - 总线错误协助

c - 如何编写一个 C 程序来计算 n 个数字的除法和减法?

javascript - 从集合中添加或删除项目以及提升的效果

python - 如何检查数组是否在 Python 中的数组列表中?

ios - Swift : value of optional type '[Int : Int]?' not unwrapped; did you mean to use '!' or '?' ? 问题

将 char * 转换为 char

在C中将char数组转换为int