gpt4 book ai didi

字符数组初始化

转载 作者:太空宇宙 更新时间:2023-11-04 00:40:05 25 4
gpt4 key购买 nike

这是如何工作的:

char Test1[8] = {"abcde"} ;

AFAIK,这应该存储在 Test1 的内存中作为

a b c d e 0 SomeJunkValue SomeJunkValue

相反,它被存储为:

a b c d e 0 0 0

初始化仅在字符串文字后添加一个尾随 NULL 字符,但所有其他数组成员如何以及为何初始化为 NULL ?此外,关于什么是底层方法或函数的任何链接或任何概念性想法:char TEST1[8] = {"abcde"} ; 将非常有帮助。怎么样:

char Test1[8] = {"abcde"} ;

不同于

char Test1[8] = "abcde" ;

?

最佳答案

部分初始化聚合的未指定成员被初始化为该类型的零。

6.7.9 Initialization

21 - 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.

10 - [...] If an object that has static or thread storage duration is not initialized explicitly, then:

  • if it has pointer type, it is initialized to a null pointer;
  • if it has arithmetic type, it is initialized to (positive or unsigned) zero; [...]

对于数组 char Test1[8],根据 6.7,初始化器 {"abcde"}"abcde" 完全等效。 9:14:

An array of character type may be initialized by a character string literal or UTF−8 string literal, optionally enclosed in braces.

关于字符数组初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12596796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com