gpt4 book ai didi

c - 为什么我不能在 IF-Else 中声明 char 数组?

转载 作者:太空狗 更新时间:2023-10-29 16:12:42 25 4
gpt4 key购买 nike

我试图根据文件中的行数在 if-else 中给出 Char 数组的大小。但是当我之后尝试使用它时,它给出了错误:“array undeclared”

FILE *f=fopen("G:\\workspaceC\\small1.txt","r");

while((c=fgetc(f))!=EOF)
{
if(c=='\n')
no_of_lines++;
}
printf("no_of_lines: %d",no_of_lines);
int fclose(FILE *f);

if(no_of_lines<10){
char b[30];
}
else if(no_of_lines>10 && no_of_lines<15){
char b[60];
}
else{
char b[106];
}

for(z=0;z<size;z++)
{
if(c==b[z]) ///////Here it gives error: "b undeclared"
{
flag=1;
break;
}
}

最佳答案

您已经在 if block 中声明了数组 b。因此,它仅对 if block 可见。而且,它在 if block 之外是不可见的。

如果你改变代码如下,你会得到你想要的。

int size;
if(no_of_lines<10){
size = 30;
}
else if(no_of_lines>10 && no_of_lines<15){
size = 60;
}
else{
size = 106;
}
char b[size];

关于c - 为什么我不能在 IF-Else 中声明 char 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21360784/

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