Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.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 我能';t在回调函数中将字符串拆分为字符串数组_C - Fatal编程技术网

C 我能';t在回调函数中将字符串拆分为字符串数组

C 我能';t在回调函数中将字符串拆分为字符串数组,c,C,在回调函数中(意味着我将不断地重新定义变量),我接收到一个需要拆分并存储到数组中的字符数组 我已将char*array[14]声明为全局变量,在回调函数中我有: int i = 0; char* p = strtok(read, ","); while (p != NULL) { array[i++] = p; p = strtok(NULL, ","); } 当我尝试在回调函数之

在回调函数中(意味着我将不断地重新定义变量),我接收到一个需要拆分并存储到数组中的字符数组

我已将char*array[14]声明为全局变量,在回调函数中我有:

    int i = 0;
    char* p = strtok(read, ",");

    while (p != NULL)
    {
        array[i++] = p;
        p = strtok(NULL, ",");
    }
当我尝试在回调函数之外访问该数组时,会得到垃圾值
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int main(){
char read[18]="1,2,3,4,5,6,7,8,9";
 int i = 0;
 char array[10][2];
 char* p=strtok(read, ",");

while (p != NULL)
{
    
    strcpy(array[i], p);
    p = strtok(NULL, ",");
    printf("%s\n",array[i]);
    i++;
}
 return 0;
}
#包括 #包括 int main(){ 字符读取[18]=“1,2,3,4,5,6,7,8,9”; int i=0; 字符数组[10][2]; char*p=strtok(读“,”); while(p!=NULL) { strcpy(数组[i],p); p=strtok(空,“,”); printf(“%s\n”,数组[i]); i++; } 返回0; }
改用strcpy函数复制字符串。我收到访问冲突写入位置错误,请发布重新编辑的代码I替换的数组[I++]=p;使用strcpy(数组[i++],p);strcpy将数组作为第一个参数,因此数组必须具有2维。