【发布时间】:2021-10-29 09:44:08
【问题描述】:
我想将数组 string[1000][1000] 中的所有字符串与字符串中超过 10 个字母进行比较。所以在我的代码中,我使用嵌套循环尝试将其与数组中的其他字符串进行比较。但它说代码给我这个错误“未知类型名称'count'”,“[Error] 预期的声明说明符或'...'在字符串常量之前”和“[Error] 预期的标识符或'('之前' }' 令牌" 为什么会一直这样?
void main()
{
char string[1000][1000];
int i, o = 1, sentence;
printf("enter the number of speeches: ");
scanf("%d", &sentence);
for (i = 0; i < sentence; i++)
{
printf("\nplease enter speech number %d: ", o);
scanf("%s", &string[i]);
o++;
}
int q, w, t, count = 0;
for(q = 0; q < sentence; q++)
{
t = strlen(string[q]);
if (t >= 10)
{
if (q = 0)
{
count++;
}
else
{
for(w = 0; w < q; w++)
{
if (strcmp(string[q], string[w]) == 0)
{
count += 0;
break;
}
else
count++;
}
}
}
}
}
printf("%d", count);
}
【问题讨论】:
-
在这两个
}中具有相同缩进的可能是错误的。 -
不相关:
scanf("%s", string[i]);(没有&)和if (q == 0)(有比较而不是赋值) -
count += 0;没有任何用处,你不需要它。 -
此外,您的数组
string占用了几乎整个 MiB 的内存,这使得它非常接近 Windows 上的堆栈限制(默认情况下,它只为每个进程提供一个 MiB 堆栈)。在读取sentence的值后使用指针和动态分配,或者可能使用可变长度数组。 -
一致的代码风格真的很有帮助。这包括缩进,如果您要在
if上使用花括号,请在相应的else上使用它们。
标签: c debugging multidimensional-array error-handling nested