下面的程序给出了所需的输出(计算连续 3 行的单词数) 但它给出了“运行时检查失败 #2 - 变量‘str’周围的堆栈已损坏” 并挂起。我试过了,但找不到解决方案。谢谢
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int i,count=0;
int main(void){
char str[3][1000];
char *ptr;
//Get user input
puts("Enter three lines:");
for (i = 0; i < 3; i++)
{
gets(&str[i][1000]);
}
for (i = 0; i < 3; i++)
{
ptr=strtok(&str[i][1000]," ");
count++;
while (ptr!=NULL)
{
ptr=strtok(NULL, " ");
if (ptr!=NULL)
{
count++;
}
}
}
printf("%d words", count);
getch();
}
最佳答案
这一行gets(&str[i][1000]);
应该是
gets(str[i]);
但由于 gets()
已被弃用,您应该真正使用
fgets (str[i], 1000, stdin);
类似的错误出现在这一行 ptr=strtok(&str[i][1000],"");
关于c - 运行时检查失败 #2 - 变量 'str' 周围的堆栈已损坏。错误?如何纠正?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18766767/