Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在不同的变量中拆分字符串_C_Strtok_Strcpy - Fatal编程技术网

C 在不同的变量中拆分字符串

C 在不同的变量中拆分字符串,c,strtok,strcpy,C,Strtok,Strcpy,我有一个字符串,比如: char string = " dog beans 1234 cat rice 0123 bird peanut 7777" 我希望将其拆分为不同的变量,如: char animals[1000], food[1000], numbers[1000]; 我到现在为止所做的: int i; while (string != NULL) { for (i = 0, i < 1000, i++) { strcpy(animals[i], st

我有一个字符串,比如:

char string = " dog beans 1234 cat rice 0123 bird peanut 7777"
我希望将其拆分为不同的变量,如:

char animals[1000], food[1000], numbers[1000];
我到现在为止所做的:

int i;
while (string != NULL)  {
    for (i = 0, i < 1000, i++) {
        strcpy(animals[i], strtok(string, " ");
        strcpy(food[i], strtok(string, " ");
        strcpy(numbers[i], strtok(string, " ");
    }
}
第一个循环工作正常,但第二个循环出现分段错误。

首先,您将动物、食物和数字视为字符串数组,但将它们声明为字符数组。您需要将它们声明为char数组的数组,或者声明为char指针的数组,在这种情况下,您需要使用malloc分配字符串

其次,当您使用相同的非空初始参数调用strtok时,您将得到相同的值。您需要在初始调用之后的所有调用中传递NULL:

strcpy(animals[i], strtok(string, " "));
strcpy(food[i], strtok(NULL, " "));
strcpy(numbers[i], strtok(NULL, " "));

首先,当您读取多达1000个动物名称时,您需要1000个动物元素[99],假设单个动物名称的最大长度为99,而不是所有动物名称的最大长度为1000

其次,strtok总是从初始字符串开始,总是产生相同的值。仅对第一次调用使用字符串,对所有其他调用使用NULL

进一步注意,如果strtok不能提取另一个令牌,它将返回NULL;这也可能发生在循环中,例如,strcpyfood[i],strtok。。。可能会崩溃。因此,我建议在进一步使用之前检查strtok的返回值:

char animals[1000][99];
char food[1000][99];
char numbers[1000][99];
int  i=0;
char *token = strtok(string, " ");
while (i < 1000 && token)
  strcpy (animals[i], token);
  token = strtok(NULL, " ");
  if (!token) 
    break;
  strcpy (food[i], token);
  token = strtok(NULL, " ");
  if (!token) 
    break;
  strcpy (numbers[i], token);
  token = strtok(NULL, " ");
  i++;
}

你的代码无法编译。调用strcpyanimals[i],strtokstring;在分号前缺少一个。和char字符串=…;也不编译!当心使用char*string,因为这样做会破坏字符串文字-不好,请不要发布未编译的代码,除非您正在寻求编译错误的帮助-这会让人恼火。如果您将所需内容显示为输出,这也会很有帮助。如果您想让动物数组容纳狗猫鸟,让数字数组容纳1234 0123 7777,那么只能用一种方法。如果你想要指针数组,那么你可以用另一种方式。如果您创建MCVE,它也会有所帮助。显然,这不是您正在运行的代码的副本,因为它无法编译。在外部循环的第一次迭代之后,第二次迭代需要第一个strtok使用NULL,而不是string。对原始代码进行了巨大的改进,但仍然不安全地使用strcpy。
char animals[1000][99];
char food[1000][99];
char numbers[1000][99];
int  i=0;
char *token = strtok(string, " ");
while (i < 1000 && token)
  strcpy (animals[i], token);
  token = strtok(NULL, " ");
  if (!token) 
    break;
  strcpy (food[i], token);
  token = strtok(NULL, " ");
  if (!token) 
    break;
  strcpy (numbers[i], token);
  token = strtok(NULL, " ");
  i++;
}