c - 运行时检查失败 #2 - 变量 'str' 周围的堆栈已损坏。错误?如何纠正?

标签 c

下面的程序给出了所需的输出(计算连续 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/

相关文章:

c - C语言如何拦截静态库调用?

c - 为什么这段代码有错误?是因为大括号吗?

c - zlib 函数声明中 'OF' 的含义

c - 为什么我的 C 代码会跳过下一个用户输入?

c - 访问和修改局部静态变量

c - 使用 void 指针和计算的偏移量为结构成员赋值

c - 未定义对 `sctp_get_no_strms' 的引用

c - Visual Studio 2015 中的宏 : expected an expression

c++ - 使用strcat时访问冲突写入位置错误

c - Pthread程序死锁使用条件变量