gpt4 book ai didi

c - 零长度数组有效,我无法更改它

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

我发现我的代码中有错误(C)。我声明了零长度数组,并且它有效。问题是我不知道这危险吗?我尝试将其更改为

char c[1];
c[0] = '\0'; //or char c[1] = {""};

但是之后就不起作用了。这是我的代码:

void TranslateFile(struct keyElem* guard,FILE* inpFile,FILE* outFile)//"tłumaczy" plik spowrotem
{
char x;
char c[0] = {""};
char tab[100] = {""};
char empty[100] = {""};

while((feof(inpFile)==0))
{
fscanf(inpFile,"%c",&x);
if(x==' ')
{
c[0] = SearchChar(guard,tab);//get char from list
fprintf(outFile,"%s",c);
strcpy(tab,empty);
x = '\0'; //puts(tab);
}
if(x != ' ')
{
c[0] = x;
strcat(tab,c);
}
}
}

我不知道它是否保存,也不知道如何更改它。

最佳答案

  1. 不允许使用零长度数组。您使用哪个编译器?
  2. 在 fprintf 中使用 %c 代替 %s。 %s 需要一个以零结尾的 char*,但您的代码有一个 char[1]。当 C[1] 为“/0”时,它可能在调试版本中工作。但在发布版本中,程序可能会向输出文件写入很长的字符串,直到内存中出现“/0”为止。如果必须使用 char 数组,请在所有情况下声明 char[2] 并设置 c[1] = '\0'。
  3. “if (x != ' ')”逻辑有点奇怪。第一个 if 中有 x = '\0'。

关于c - 零长度数组有效,我无法更改它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44077984/

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