C 中的 gets() 问题

发布于 2024-11-01 05:08:55 字数 652 浏览 2 评论 0原文

我编写了以下代码:

#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' 的增量”。

好像出了什么问题?

感谢 !

I wrote the following code:

#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;
}

The problem is that the gcc gives me an error for the 'int const count': " increment of read-only variable ‘count’".

What seems to be wrong ?

Thank !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

对不⑦ 2024-11-08 05:08:55

尝试使用 fgets 代替:

fgets (string, SIZE, stdin);

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

Try using fgets instead as:

fgets (string, SIZE, stdin);

Why gets is unsafe, has been answered several times on SO. You can see this.

楠木可依 2024-11-08 05:08:55

始终使用 fgets() 而不是 gets。还有很多东西需要修复。您不应该使用标准库函数来创建用户界面。标准库确实不是为此设计的。相反,您应该使用 curses 库 或类似的东西。您还可以编写程序来接受参数作为输入。

正确使用标准库的简短示例。该版本没有任何错误检查,因此假设用户输入是正确的。

#include <stdio.h>

int main(int artc, char *argv[])
{
    /* arguments are strings so assign only the first characte of the
     * third argument string. Remember that the first argument ( argv[0] ) 
     * is the name of the program. 
     */
    char  mychar = argv[2][0];
    char *string = argv[1];
    int i, count = 0;

    /* count the occurences of the given character */
    for(; *string != '\0'; ++string)
        if(*string == mychar) ++count;

    printf("The char ‘%c’ appears %d times.\n", mychar, count);

    return 0;
}

用法: ./count "Hello, World!" l

输出: 字符“l”出现3次。


编辑:至于原始代码。将 == 更改为 !=

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

to:

for (i=0 ; (string[i] != '\0') ; i++ )

比较是错误的。

Always use fgets() instead of gets. Also there are lots of stuff to fix. You shouldnt use standard library functions for creating user interface. Standard library is really not designed for that. Instead you should use curses library or something similar. You could also write the program to accept arguments as input.

Short example of proper use of the standard library. This version does not have any error checking so it assumes that user input is correct.

#include <stdio.h>

int main(int artc, char *argv[])
{
    /* arguments are strings so assign only the first characte of the
     * third argument string. Remember that the first argument ( argv[0] ) 
     * is the name of the program. 
     */
    char  mychar = argv[2][0];
    char *string = argv[1];
    int i, count = 0;

    /* count the occurences of the given character */
    for(; *string != '\0'; ++string)
        if(*string == mychar) ++count;

    printf("The char ‘%c’ appears %d times.\n", mychar, count);

    return 0;
}

Usage: ./count "Hello, World!" l

Output: The char ‘l’ appears 3 times.


EDIT: As for the original code. Change == to !=.

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

to:

for (i=0 ; (string[i] != '\0') ; i++ )

The comparison was wrong.

少女情怀诗 2024-11-08 05:08:55

要使此示例正常工作,您还应该将以下行:

if(*string == mychar) ++count;

更改为

if(string[i] == mychar) ++count;< /code>

完整的工作示例现在是:

#include <stdio.h>

int main(int artc, char *argv[])
{
/* arguments are strings so assign only the first characte of the
 * third argument string. Remember that the first argument ( argv[0] ) 
 * is the name of the program. 
 */
char  mychar = argv[2][0];
char *string = argv[1];
int i, count = 0;

/* count the occurences of the given character */
for (i=0 ; (string[i] != '\0') ; i++ )
    if(string[i] == mychar) ++count;

printf("The char ‘%c’ appears %d times in the sentence: %s\n", mychar, count, string);

return 0;
}

To make this example work you should also change the line:

if(*string == mychar) ++count;

into

if(string[i] == mychar) ++count;

Full working example is now:

#include <stdio.h>

int main(int artc, char *argv[])
{
/* arguments are strings so assign only the first characte of the
 * third argument string. Remember that the first argument ( argv[0] ) 
 * is the name of the program. 
 */
char  mychar = argv[2][0];
char *string = argv[1];
int i, count = 0;

/* count the occurences of the given character */
for (i=0 ; (string[i] != '\0') ; i++ )
    if(string[i] == mychar) ++count;

printf("The char ‘%c’ appears %d times in the sentence: %s\n", mychar, count, string);

return 0;
}
狼性发作 2024-11-08 05:08:55

考虑替换为“scanf(“%s”, &string)”。

Consider replace with "scanf( "%s", &string)" instead.

转瞬即逝 2024-11-08 05:08:55

gets 是危险的,因为它允许您读入的数据多于分配的空间,您可以使用 fgets 来指定要读入的字符数,并在发现换行符时停止。

gets is dangerous because it lets you read in more data than you've allocated space for, you can use fgets which specifies how many characters it is going to read in and stops if it finds a newline.

遗失的美好 2024-11-08 05:08:55

gets 是危险的,因为它可以获取比变量大小更多的数据。从而使系统遭受攻击并损害安全性。
应该使用 fgets,因为它限制了数量。要读取的字符数。

gets is dangerous because it can take in more data than the size of the variable. Thereby exposing the system to attacks and compromising security.
fgets should be used as it limits no. of characters to be read.

謌踐踏愛綪 2024-11-08 05:08:55

这会做:

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

#define SIZE 128

int main()
{
  char mychar, string[SIZE];
  int i;
  int count=0;    

  printf("Please enter your string: ");
  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 in the sentence: %s" ,mychar ,count, string);

  return 0;
}

This will do:

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

#define SIZE 128

int main()
{
  char mychar, string[SIZE];
  int i;
  int count=0;    

  printf("Please enter your string: ");
  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 in the sentence: %s" ,mychar ,count, string);

  return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文