在 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/