C 拆分字符串并将其存储到字符串数组中
作为一个旨在存储字典并根据字典替换单词的程序的一部分,我编写了一个函数,该函数基本上将(使用C 拆分字符串并将其存储到字符串数组中,c,strtok,C,Strtok,作为一个旨在存储字典并根据字典替换单词的程序的一部分,我编写了一个函数,该函数基本上将(使用strtok)字符串拆分为单词(用空格分隔),并将每个单词存储到字符串数组中。 代码如下: void StoreArr(char * original,char ** dest) { int i=0; char * token =strtok(original, " "); dest[
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)];
目的地的长度是字数。
一旦程序运行,它会自动终止,而不显示文本
使用StoreArr(文件,目标)调用它代码>
编辑:
?我收到一条警告“正在从不兼容的指针类型[默认情况下已启用]传递'StoreArr'的参数1,2”
提前谢谢
p、 s:
上一篇文章中的代码与我的代码相同,但我的代码不起作用。我怀疑这两行代码构成了一个问题,我不知道为什么:
dest[i]=malloc(sizeof(token));
strcpy(dest[i],token);
如果看不到更多的代码,代码中可能有未定义的行为
通过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;
}
size\u t StoreArr(字符*原件,字符***目的地)
{
如果(原始==NULL | |目的地==NULL)
返回0;
大小\u t大小=0;
char*token=strtok(原件,“”);
while(令牌!=NULL)
{
/*(重新)分配阵列*/
char**temp=realloc(*目的地,sizeof(char*)*(size+1));
if(temp==NULL)
{
/*分配失败,释放到目前为止的资源*/
如果(*目的地!=NULL)
{
对于(大小i=0;i
现在,该函数会根据需要动态分配新条目,并返回数组中的条目数,或在出现错误时返回0
。如果您还没有,则启用更多警告。警告通常是你正在做不应该做的事情的标志。另外,请发布一个完整的例子,解释什么是Length
和Count
。最好是一个.和最后一件事,在strtok
不返回NULL
时循环可能会更好。例如,while(token!=NULL){…}
我添加了这两个函数,并将其更改为while(即使在本例中它不相关)。还有其他解决方案吗?我收到一条警告“从不兼容的指针类型[默认启用]传递'StoreArr'的参数2”,我收到一条警告“从不兼容的指针类型[enabled by default]传递'StoreArr'的参数1,2”非常感谢,但我的目标是解决我当前的代码(我想从错误中学习)。至于更改字符串的strtok-我不介意,我只需要它将内容传输到数组中。目标应该是字符串数组(字符数组)。我不能避免分配吗?@user3005945您可以通过使用数组数组来避免显式分配,如char Destination[X][Y]
,这是唯一的方法。好的。如果我确实使用分配,我如何更改我现有的代码?我应该为每个单元格添加分配吗?另外,我的目的地是否被视为字符串数组?@user3005945如果dest
已经是正确分配的字符串数组,那么只需使用strdup
复制每个字符串。好的。那么我的问题是:如何创建一个适当的字符串数组,以及为什么要复制每个字符串?我只想从原始字符串(char*)中移动每个拆分的单词我确实知道我需要存储的字符串的数量,这就是为什么我的目标数组设置了一个大小。再次,我为我的无知道歉,我是c语言的初学者,只是想从经验中学习。
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;
}