【问题标题】:Split a string and store into an array of strings拆分字符串并存储到字符串数组中
【发布时间】:2013-11-24 13:23:36
【问题描述】:

作为旨在存储字典并根据它替换单词的程序的一部分,我编写了一个函数,该函数基本上将(使用strtok)一个字符串拆分为它的单词(由空格分隔),并将存储每个单词转换成字符串数组。 代码如下:

void StoreArr(char * original,char ** dest)
            {
                int i=0;

                char * token =strtok(original, " ");
                dest[i]=malloc(sizeof(token));
                strcpy(dest[i],token);
                ++i;

                while(token!=NULL)
                {
                        dest[i]=malloc(sizeof(token));
                        strcpy(dest[i],token);
                        printf("%s",token);
                        token =strtok(NULL, " ");
                        ++i;
                }

            }

我传递了以下变量:

         char * File = "";
         File=malloc(Length(Text)*(sizeof(char)));
         char ** Destination[Count(' ',File)];

destination 的长度是单词的数量。 一旦程序运行,它会自行终止而不显示文本 使用StoreArr(File,Destination);调用它

编辑:

int Length(FILE * file)
            {
                 long result;
                    long origPos = ftell(file);//original start position of file
                    fseek(file, 0, SEEK_END);//move to end of file
                    result = ftell(file);//return value at end
                    fseek(file, origPos, SEEK_SET);//rewind to beginning
                    return result;
            }
            int Count(char a,char * b)
            {
                int i=0;
                int count=0;
                while(b[i]!='\0')
                {
                if(b[i]==a || b[i]=='\n')
                    ++count;
                ++i;
                }
                return count+1;
            }

?我收到警告“从不兼容的指针类型传递 'StoreArr' 的参数 1,2 [默认启用]” 提前致谢。

ps:Breaking down string and storing it in array

上一篇文章的代码和我的一样,但是我的不行。我怀疑这两行有问题,我不知道为什么:

 dest[i]=malloc(sizeof(token));
                    strcpy(dest[i],token);

【问题讨论】:

  • 启用更多警告(如果您还没有)。警告经常表明你正在做一些你不应该做的事情。
  • 另外,请发布一个完整示例,解释LengthCount 是什么。最好是SSCCE
  • 最后一件事,在strtok 不返回NULL 时循环可能会更好。例如。 while (token != NULL) { ... }
  • 我添加了 2 个函数,并将其更改为 while(即使在这种情况下它不相关)。还有其他解决方案吗?我收到警告“传递 'StoreArr 的参数 2 ' 来自不兼容的指针类型 [默认启用]"
  • 我收到一条警告“从不兼容的指针类型传递 'StoreArr' 的参数 1,2 [默认启用]”

标签: c strtok


【解决方案1】:

如果没有看到更多代码,您的代码中可能会有未定义的行为

通过Destination 的声明,您有一个指向指针的指针数组,我不知道这是否是您想要的。您还需要分配所有指针。

您还必须小心 strtok 修改它标记的字符串,因此您不能传递文字或常量字符串。


如果是我,我会这样做:

char **Destination = NULL;
StoreArr(Original, &Destination);

还有这样的功能

size_t StoreArr(char *original, char ***destination)
{
    if (original == NULL || destination == NULL)
        return 0;

    size_t size = 0;
    char *token = strtok(original, " ");
    while (token != NULL)
    {
        /* (re)allocate array */
        char **temp = realloc(*destination, sizeof(char *) * (size + 1));
        if (temp == NULL)
        {
            /* Allocation failed, free what we have so far */
            if (*destination != NULL)
            {
                for (size_t i = 0; i < size; ++i)
                    free((*destination)[i]);
                free(*destination);
            }

            return 0;
        }

        /* Set the destination pointer */
        *destination = temp;

        /* Duplicate the token */
        (*destination)[size++] = strdup(token);

        /* Get next token */
        token = strtok(NULL, " ");
    }

    return size;
}

该函数现在可以根据需要动态分配新条目,并返回数组中的条目数或0(出错时)。

【讨论】:

  • 非常感谢,但我的目标是解决我当前的代码(因为我想从错误中吸取教训)。至于 strtok 更改字符串 - 我不介意,我只需要它将内容传输到数组中。目标应该是字符串数组(字符数组)。我不能避免分配吗?
  • @user3005945 您可以通过数组数组来避免显式分配,例如char Destination[X][Y];,这是唯一的方法。
  • 好的。如果我确实使用了分配,我该如何更改我现有的代码?我应该简单地为每个单元格添加一个分配吗?另外,我的目标是否被视为字符串数组?
  • @user3005945 如果dest 已经是正确分配的字符串数组,那么只需使用strdup 复制每个字符串。
  • 好的。所以我的问题是:我如何创建一个正确分配的字符串数组,为什么要复制每个?我只是想从原始字符串中移动每个拆分词( char *) 到数组中的每个单元格中。我知道我需要存储的字符串的数量,这就是为什么我的目标数组设置了一个大小。再次,我为无知道歉,我是 c 的初学者,只是想从体验中学习。
猜你喜欢
  • 2017-03-07
  • 1970-01-01
  • 1970-01-01
  • 2016-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-13
相关资源
最近更新 更多