【问题标题】:Convert Char* to array of Char*将 Char* 转换为 Char* 数组
【发布时间】:2013-06-06 12:30:45
【问题描述】:

我正在尝试将 Char* 转换为 Char**

例如"echo Hello World" 将变为 {"echo", "Hello", "World"}

我知道,我可以从 Char*strtok() 获取单个单词。

但我在初始化 Char** 时遇到问题,因为 Char* 的大小未知,并且单个单词的大小也未知。

【问题讨论】:

  • 我假设您的意思是“char”而不是“Char”。 char** 应该指向一个指针数组,每个指针都指向一个字符串。换句话说 char** wordlist = malloc( sizeof( char* ) * iWordCount )
  • 嗯,所以我需要创建一个 char* 的副本,首先计算单词然后插入它们,是吗?

标签: c arrays string converter


【解决方案1】:

您的 char** 只是指向第一个 char * 的指针(或者,char 指针数组的开头)。 char*[] 的分配(它char**!! 相同)可能是一个更大的问题。 你应该使用malloc 来完成这个任务。如果您事先不知道char*s 的数量,您可以猜测一些大小,将其填入NULLs 并在需要时调用realloc

【讨论】:

    【解决方案2】:

    您可以在您的字符串上运行并搜索“”(空格字符),然后您找到的每个空格都可以使用函数strncpy 获取子字符串,以获取当前空间索引和最后一个空间索引之间的字符串。您创建的每个字符串都可以存储在“动态”数组中(使用 malloc 和 realloc)。
    对于第一个子字符串,您的起始索引为 0,在字符串的末尾,您将获得最后一个空格索引和字符串长度之间的最后一个子字符串。

    【讨论】:

      【解决方案3】:

      Google search 中的第一个结果为您提供了一个可以修改的示例:

      /* strtok example */
      #include <stdio.h>
      #include <string.h>
      #include <stdlib.h>        
      
      int main ()
      {
        // allocate 10 strings at a time
        int size = 10;
        int i = 0;
        char str[] ="echo Hello World";
        char** strings = malloc(size * sizeof(char*));
        char* temp;
      
        printf ("Splitting string \"%s\" into tokens:\n",str);
        temp = strtok (str," ");
        while (temp != NULL)
        {
          strings[i++] = temp;
          temp = strtok (NULL, " ,.-");
          if(i % size == 0)
              //allocate room for 10 more strings
              strings = realloc(strings, (i+size) * sizeof(char*));
        }
      
        int j;
        for(j = 0; j < i; j ++) 
        {
            printf ("%s\n",strings[j]);
        }
        return 0;
      }
      

      【讨论】:

      • strtok() 是一个合理的建议,但应附带警告它会修改原始字符串。
      【解决方案4】:
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include <ctype.h>
      
      size_t word_count(const char *str){
          enum { out, in } status;
          size_t count = 0;
          status = out;
          while(*str){
              if(isspace(*str++)){
                  status = out;
              } else if(status == out){
                  status = in;
                  ++count;
              }
          }
          return count;
      }
      
      int main(void){
          char original[] = "echo Hello World";
          size_t i, size = word_count(original);
          char *p, **words = (char**)malloc(sizeof(char*)*size);
      
          for(i = 0, p = original;NULL!=(p=strtok(p, " \t\n")); p = NULL)
              words[i++] = p;
          //check print
          printf("{ ");
          for(i = 0;i<size;++i){
              printf("\"%s\"", words[i]);
              if(i < size - 1)
                  printf(", ");
          }
          printf(" }\n");
      
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2011-06-08
        • 2016-03-10
        • 2017-05-30
        • 2022-01-22
        • 1970-01-01
        • 1970-01-01
        • 2010-10-12
        • 2021-04-29
        • 2013-10-18
        相关资源
        最近更新 更多