Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.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
strcpy时的Seg故障_C - Fatal编程技术网

strcpy时的Seg故障

strcpy时的Seg故障,c,C,这是我正在研究的相关代码。我将stdin中的输入标记化,没有任何问题,当我复制该输入时,会得到一个segfault。但是,“strcpy(s,input)”没有错误。我是不是错过了一些基本的东西?多谢各位 char *s = malloc(64 * sizeof(char)); char *token = malloc(64 * sizeof(char)); char *currstring = malloc(128 * sizeof(char)); currstring = NULL; fge

这是我正在研究的相关代码。我将stdin中的输入标记化,没有任何问题,当我复制该输入时,会得到一个segfault。但是,“strcpy(s,input)”没有错误。我是不是错过了一些基本的东西?多谢各位

char *s = malloc(64 * sizeof(char));
char *token = malloc(64 * sizeof(char));
char *currstring = malloc(128 * sizeof(char));
currstring = NULL;
fgets(input,100, stdin);
strcpy(s, input);
token = strtok(s,delim);

while (token) 
{
    //Condition checking      
     strcpy(currstring,token);
}
不过,这可能没有必要。如果以后要执行
strcpy()
,则不需要将字符串设置为
“”


您也不需要为
令牌
分配内存
strtok()
将导致
token
指向
s
中的某个地方,因此为
token
分配内存只会在执行
token=strtok(s,delim)操作后泄漏内存

OP还将最多100个字符读入
input
(其定义未显示),然后将内容复制到
s
,其中仅包含64个字节。
char *currstring = malloc(128 * sizeof(char));
currstring = NULL;
currstring[0] = '\0';
// or
strcpy(currstring, "");
char *token = malloc(64 * sizeof(char));