gpt4 book ai didi

c - 使用 strtok 的段错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:24:33 25 4
gpt4 key购买 nike

<分区>

尝试使用分隔符“”使用 strtok 拆分字符串,但出现段错误。

    char command[12];
char *instruction;
char *parameter_1;
char *parameter_2;
*instruction = strtok(command, " ,.-");
*parameter_1 = strtok(NULL, " ,.-");
*parameter_2 = strtok(NULL, " ,.-");
write(1,parameter_1,sizeof(parameter_1));

在那个点

编辑:根据要求,编译器警告。没有在任何地方使用 int,所以很困惑。

shell.c:51:16: warning: assignment makes integer from pointer without a cast [enabled by default]
shell.c:52:16: warning: assignment makes integer from pointer without a cast [enabled by default]
shell.c:53:16: warning: assignment makes integer from pointer without a cast [enabled by default]

Command: abd def ghj
Segmentation fault (core dumped)

编辑 2:

int prompt() {
// Get Input
char inputData[256];
int rid;
rid = read(0,inputData,256);

// Strip input
char command[rid];
int i;
for (i = 0; i<=rid-2; i++) {
command[i] = inputData[i];
}
command[rid-1] = '\0';

// Debug
// printf("\n-%c-%i\n",command[10],(int)sizeof(command));
// write(1,command,sizeof(command));

if (strcmp(command, "exit") == 0) {
break;
}

char *instruction;
char *parameter_1;
char *parameter_2;
instruction = strtok(command, " ");
parameter_1 = strtok(NULL, " ");
parameter_2 = strtok(NULL, " ");
write(1,instruction,sizeof(instruction));
//write(1,parameter_2,sizeof(parameter_2));
}

return 0;

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com