来自用户输入的动态指针数组

Dynamic array of pointers from user input

关于 C 中指针数组的动态分配,我需要一些帮助。我正在尝试创建一个程序,从用户输入中读取单词的句子,并将单词存储在字符数组字符串中。然后我想将指向这些单词的指针 char *word 保存在指针数组 char **wordArray.

为单词创建一个动态分配的工作方法很容易,它从用户输入中逐个字符地读取。但是,尝试将此方法应用于指针数组比较棘手。

目前的功能char **varArray明显有缺陷,但我的想法是"while the user has input, get words pointers for the array of pointers"。它现在有效地循环每个 char c 的第一个单词。

我的问题是,如何实现指针数组的第二层 (char **varArray()) 动态内存分配?该函数如何检测何时调用 char *word()?

对于代码、样式或其他错误的反馈当然非常感谢。我的水平是中级初学者。

/*CREATES AND ALLOCATES DYNAMIC VARIABLE ARRAY*/
#include <stdio.h>
#include <stdlib.h>

char **varArray();
char *word();

char **varArray()
{
  char **tmp=NULL;
  char **wordArray=NULL;
  size_t size=0;
  char c = EOF;
  int words=0;

  while(c) {
    c=getc(stdin);
    if (c == EOF || c == '\n')
      c=0;

    if (size <= words) {
      size+=sizeof(char *);
      tmp = realloc(wordArray,size);

      if(tmp == NULL) {
        free(wordArray);
        wordArray=NULL;
        printf("Memory allocation failed. Aborted.\n");
        break;
      }

      wordArray=tmp;
    }
    words++;
    wordArray[words]= word();
    return wordArray;
  } 

ONE的检索方法:

/*GETS ONE WORD FROM USER INPUT*/
  char *word()
{
  char *word=NULL, *tmp=NULL;
  size_t size=0;
  char c = EOF;
  int letters=0;

  while(c) { //reads character by character
    c=getc(stdin);

    if (c == EOF || c == '\n' || c==' ') //remove ' ' to read all input
      c =0;

    if (size <= letters) { //increase and reallocate memory
      size = size + sizeof(char);
      tmp = realloc(word,size);

      if (tmp==NULL) { //check if allocation failed
        free(word);
        word=NULL;
        printf("Memory allocation failed. Aborted.\n");
        break;
      }
      word= tmp;
    }
    letters=letters+1; 
    word[letters]=c;

  }
  /*ADD SENTINEL CHARACTER*/
  letters++; 
  size += sizeof(char);
  word = realloc(word,size);
  word[letters]='\n';
  return word;
}

这是您要编写的程序的框架。

 ...
     char* currentWord;
     char **wordArray=NULL;
     while ((currentWord = word()) != NULL) {
        .... add current word to word array with realloc...
     }
 ....

 char* word() {
     int ch;
     char* outputWord = NULL;
     while ((ch = getch()) != EOF) {
        if ( ... ch is a word character ... )
             ... add ch to output word with realloc ...
         else {
             char* ret = outputWord;
             outputWord = NULL;
             return ret;
        }
     }
     return NULL;
 }

请注意两个 while 循环如何做完全相同的事情。

  while ((element = getNextElement()) != sentinelValue) {  
      .... process newly obtained element ....
  }

我现在已经成功实现了@n.m提供的shell版本。然而,另一个问题出现了——由于 word() 依赖于哨兵换行符 \n 退出,它也无法读取最后一个单词,并且没有进入循环的最后一个关键时刻。

我已经尝试实现一些 if-cases,但是这些当然由于 while-condition 而失败了。另一个想法是实现一些 switch case,但我不确定这是否会避免 while 循环的麻烦?

请注意,代码几乎没有错误检查以尽量减少混乱。

  char **wordArray() {
  char *currentWord;
  char **wordArray=NULL;
  size_t size=0;
  int i=0;

  while((currentWord = word()) != NULL) {
    size+=sizeof(char *);
    wordArray=(char **) realloc(wordArray,size);
    wordArray[i]=currentWord;
    printf("Test - Current word: %s\n",currentWord);
    i++;

  }
  return wordArray;
}

相关word()函数:

char *word() {
  char ch;
  int i=0;
  size_t size=0;
  char *returnWord = NULL;
  char *outputWord = NULL;
  char *tmp = NULL;

  while((ch = getc(stdin)) != EOF && ch !='\n') { //&& ch !='\n'
    if (ch != ' ' ) { //&& ch !='\n'
      size += sizeof(char);
      tmp = (char *) realloc(outputWord,size);
        outputWord= tmp;
        outputWord[i]=ch;
        printf("Test1: %c\n",*(outputWord+i));
        i++;
    } else {
        printf("Test2: %s\n",outputWord);
        returnWord=outputWord;
        outputWord=NULL;
        printf("Test3: %s\n",returnWord);
      return returnWord;
    }
  }
  return NULL;
}