【发布时间】:2014-09-07 05:30:02
【问题描述】:
基本上我必须标记一个 4 列的行并将这些标记放入一个数组中,所以我在下面创建了这个函数。
char** tokeniser(char* lineToToken)
{
int i = 0;
char** tokenList = malloc(4*sizeof(char*));
char* token;
while ((token = strtok(lineToToken, " ")) != NULL && i<4)
{
tokenList[i] = malloc(strlen(token) + 1);
strcpy(tokenList[i], token);
++i;
}
return tokenList;
}
主要是我有一个简单的东西来测试它,并且只将第一个元素打印 4 次..
for(int i = 0; i<3; i++)
{
printf("%s", tokenList[i]);
}
我输入的文本文件是 “asda asdasd 23 asd”,但我只得到 asda 4 次:S
【问题讨论】:
-
strtok()不是这样工作的。
标签: c arrays string pointers memory