簡體   English   中英

在C中使用realloc逐字符讀取輸入char

[英]Reading input char by char using realloc in C

我想按字符讀取輸入char並將其作為單詞保存到char *數組中。 我不知道輸入會持續多久,所以我想動態分配內存。 當字符為空格時,程序結束。 如何使用realloc做到這一點? 有我的代碼:

#include <stdio.h>
int main(void) {
    char *word=malloc(1*sizeof(char));
    char c;
    int numOfChars=0;
    c=getchar();
    word[0]=c;
    numOfChars++;
    while((c=getchar())!=' '){
        numOfChars++;
        realloc(word,numOfChars);
        word[numofChars-1]=c;
    }
    printf("%s", word);

    return 0;
}

輸入示例: Word示例輸出: Word

為了解釋我的想法,我迅速建立了這個小程序來解釋如何使用指數增長:

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

#define INITIAL_CAPACITY 100
#define GROW_FACTOR 1.5

struct string_buffer {
  size_t capacity;
  size_t length;
  char *buffer;
};

typedef struct string_buffer string_buffer_t;

string_buffer_t *sb_init(void);
static void sb_grow(string_buffer_t *sb);
void sb_shrink(string_buffer_t *sb);
char *sb_release(string_buffer_t *sb);

void sb_append_char(string_buffer_t *sb, char c);
char *sb_peek_string(string_buffer_t *sb);

int main(void)
{
  string_buffer_t *sb=sb_init();
  int c;

  while ( (c=getchar())!=' ' && c!='\n' && c!=EOF )
    sb_append_char(sb, c);

  char *string=sb_release(sb);
  printf("string : \"%s\"\nlength : %zu\n", string, strlen(string));
  free(string);
  return 0;
}

string_buffer_t *sb_init(void)
{
  string_buffer_t *new=malloc(sizeof *new);
  if (new==NULL) exit(EXIT_FAILURE);

  new->capacity=INITIAL_CAPACITY;
  new->length=1;
  new->buffer=malloc(INITIAL_CAPACITY);
  if (new->buffer==NULL) exit(EXIT_FAILURE);
  new->buffer[0]=0;

  return new;
}

static void sb_grow(string_buffer_t *sb)
{
  char *new=realloc(sb->buffer, (size_t) (GROW_FACTOR*sb->capacity));
  if (new==NULL) exit(EXIT_FAILURE);
  sb->capacity=(size_t) (GROW_FACTOR*sb->capacity);
  sb->buffer=new;
}

void sb_shrink(string_buffer_t *sb)
{
  char *new=realloc(sb->buffer, sb->length);
  if (new==NULL) exit(EXIT_FAILURE);
  sb->buffer=new;
}

char *sb_release(string_buffer_t *sb)
{
  sb_shrink(sb);
  char *string=sb->buffer;
  free(sb);
  return string;
}

void sb_append_char(string_buffer_t *sb, char c)
{
  if (sb->capacity==sb->length) sb_grow(sb);
  sb->buffer[sb->length-1]=c;
  sb->buffer[sb->length]=0;
  sb->length=sb->length+1;
}

該程序可以如下所示。 考慮到輸入將被緩沖和填充,直到輸入也是空白字符的新行字符為止。 如果要使用格式說明符%s進行輸出,則結果字必須以零結尾。

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

int main( void )
{
    int c;
    size_t n;
    char *word;
    char *tmp;

    n = 0;
    word = malloc( n + 1 );
    word[n++] = '\0';

    printf( "Enter a word: " );

    while ( ( c = getchar() ) != EOF && !isspace( c ) && ( tmp = realloc( word, n + 1 ) ) != NULL )
    {
        word = tmp;
        word[n-1] = c;
        word[n++] = '\0';
    }

    printf( "You've entered \"%s\"\n", word );

    free( word );
}        

程序輸出可能看起來像

Enter a word: Hello
You've entered "Hello"

這會做

#include <stdio.h>
#include <stdlib.h>
int main(void) {
  char *ptr;
  char *word=malloc(1*sizeof *word);
  char c;
  int numofChars=0;
  printf("Enter string terminated by a space :");
  c=getchar();
  word[0]=c;
  numofChars++;

  while((c=getchar())!=' '){
    numofChars++;
    ptr=realloc(word,numofChars*sizeof *ptr);
    if(ptr!=NULL)
    {
      word=ptr;
      word[numofChars-1]=c;
    }
  }
  /* You need to append a null character to make it a valid string */
  numofChars++;
  ptr=realloc(word,numofChars*sizeof *ptr);
  if(ptr!=NULL)
  {
    word=ptr;
    word[numofChars-1]='\0';
  }

  printf("Word : %s\n", word);

  free(word); // Freeing word/

  return 0;
}

好吧,您可以編寫一個函數來替換

 numofChars++;
      ptr=realloc(word,numofChars*sizeof *ptr);
      if(ptr!=NULL)
      {
        word=ptr;
        word[numofChars-1]='\0';
      }

注意:不建議您這樣做

  word=realloc(word,numOfChars*sizeof(char));

因為如果realloc失敗,則可能會發生內存泄漏。 所以我在這里使用了ptr

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM