c - C 中的 gets() 问题

标签 c gcc compilation gets

我编写了以下代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 128

int main ()

{
    char mychar , string [SIZE];
    int i;
    int const count =0 ;    

    printf ("Please enter your string: \n\n");
    fgets (string, SIZE, stdin);

    printf ("Please enter char to find: ");
    mychar = getchar();

    for (i=0 ; (string[i] == '\0') ; i++ )
        if ( string[i]  == mychar )
            count++;

    printf ("The char %c appears %d times" ,mychar ,count);

    return 0;
}

问题是 gcc 给我一个关于“int const count”的错误:“只读变量“count”的增量”。

哪里出了问题?

谢谢!

最佳答案

尝试使用fgets而是作为:

fgets (string, SIZE, stdin);

为什么gets不安全,已在SO上多次回答。您可以看到this .

关于c - C 中的 gets() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5632239/

相关文章:

unix - 更改unix中GCC的编译顺序会删除文件吗?

c - 建议使用 gcc 编译器替代 gets() 函数

c - printf 动态长度/大小

c - 更新结构列表中的单元格并返回列表

c - 生成所有长度为n且设置k位的二进制字符串。(需要写在C上)

compilation - 无法编译 gearman - 配置脚本失败

asp.net - 如何将 ASP.NET 网站编译为单个 DLL 文件?

c - C语言中如何通过指针访问矩阵数组的元素?

c++ - EnumPrinters 示例的链接器问题

comments - 仅注释更改时编译代码