gpt4 book ai didi

c - 尝试复制固定长度的数组

转载 作者:行者123 更新时间:2023-11-30 16:39:16 24 4
gpt4 key购买 nike

我试图重复数组的元素,但陷入无限循环数组的大小当前为 2 ,我试图再次重复这些元素以使大小为 5,例如示例输出: char a[]="ED";试图做到 char a[]="EDEDE";代码如下

while(sizeof(a)<5)
{
for(i=0;i<2;i++){
a[fk]=a[i];
fk++;}
}

进入无限循环

最佳答案

C11 6.4.5 字符串文字(P7):

It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

因此,如果将 a 重新分配给其他内容,则不会调整大小,否则这是未定义的行为。

关于c - 尝试复制固定长度的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47109676/

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