C - 循环不会中断

发布于 2024-11-05 11:21:00 字数 982 浏览 0 评论 0原文

我希望当按下“Enter”键时循环中断。有什么建议吗?

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

#define len 20
#define limit 100

//Prototypes for functions
int read_word(char str[], int n);



int main(void)
{
  char *p;
  char word[len+1];
  int i=0, nwords = 0;

//Loop for reading in words and allocating an array 
  for (;;)
   {
      if (nwords == limit)
       {
          printf("Insufficient Space\n");
          break;
       }
      printf("Enter word: ");
      scanf("%c", &word);
      p = (char*) malloc(nwords*sizeof(char));
      p[i]= read_word(word, len);
      i++;

      if (p == NULL)
      {
          printf("Insufficient Space\n");
          break;
      }
  }

  for(i=0; i<nwords; i++)
      printf(" %s\n", p[i]);

  return 0;

  } 
int read_word(char str[], int n)
{
  char ch; 
  int i = 0;

  while((ch = getchar()) != '\n')
      if (i<n)
          str[i++] = ch; 
  str[i] = '\0';
  return i;
}

I want the loop to break when "Enter" is pressed. Any suggestions?

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

#define len 20
#define limit 100

//Prototypes for functions
int read_word(char str[], int n);



int main(void)
{
  char *p;
  char word[len+1];
  int i=0, nwords = 0;

//Loop for reading in words and allocating an array 
  for (;;)
   {
      if (nwords == limit)
       {
          printf("Insufficient Space\n");
          break;
       }
      printf("Enter word: ");
      scanf("%c", &word);
      p = (char*) malloc(nwords*sizeof(char));
      p[i]= read_word(word, len);
      i++;

      if (p == NULL)
      {
          printf("Insufficient Space\n");
          break;
      }
  }

  for(i=0; i<nwords; i++)
      printf(" %s\n", p[i]);

  return 0;

  } 
int read_word(char str[], int n)
{
  char ch; 
  int i = 0;

  while((ch = getchar()) != '\n')
      if (i<n)
          str[i++] = ch; 
  str[i] = '\0';
  return i;
}

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

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

发布评论

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

评论(1

烈酒灼喉 2024-11-12 11:21:00

您的 scanf 调用读取第一个字符,然后您的 read_word 函数会覆盖它。如果 scanf 调用读取换行符,它将被忽略。

这些行:

  p = (char*) malloc(nwords*sizeof(char));
  p[i]= read_word(word, len);

... 也显得错误。 read_word 返回一个整数(读取的字符串的长度),但您将存储到 char 数组中。此外,每次循环都会为 p 重新分配内存,因此之前存储的值将会丢失。

修复方法:

  • p 更改为 int *,并将其初始化为 null,
  • malloc 调用更改为合适的 realloc
  • 完全删除对 scanf 的调用,
  • 将对 p == null 的检查移至 `p = (char*) malloc( nwords*sizeof(char));'

或者: p 实际上是一个字符串数组(单词本身)而不是单词长度?在这种情况下,您必须:

  • p 更改为 char **
  • 将分配大小(用于 realloc 调用)更改为 nwords * sizeof(*p)
  • 为每个单词分配(使用 malloc)存储空间,而不是让 word 成为堆栈分配的数组集
  • p[ i] = word; 而不是当前的赋值。

Your scanf call reads the first character, and then your read_word function overwrites it. If the scanf call reads the newline, it will then be ignored.

The lines:

  p = (char*) malloc(nwords*sizeof(char));
  p[i]= read_word(word, len);

... also appears wrong. read_word returns an integer (the length of the string read), but you are storing into a char array. Also, you are re-allocating the memory for p each time through the loop, so the values stored previously will be lost.

To fix:

  • change p to be an int *, and initialize it to null
  • change the malloc call to a suitable realloc
  • remove the call to scanf entirely
  • move the check for p == null before the assignment of `p = (char*) malloc(nwords*sizeof(char));'

Or: is p meant to actually be an array of strings (the words themselves) rather than the word length? In that case you have to:

  • change p to be an char **
  • change the allocation size (for the realloc call) to nwords * sizeof(*p)
  • allocate (using malloc) storage for each word instead of having word be a stack-allocated array
  • set p[i] = word; rather than the current assignment.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文