strtok을 사용하여 구분 기호를 사용하여 문자열을 분할하려고 시도했지만 분할 오류가 발생합니다. 제strtok을 사용하여 분할 오류가 발생했습니다.
EDIT의 시점
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;
}
경고와 함께 컴파일하십시오. 컴파일 가능한 예제를 붙여 넣으면 부분 코드 단편에 표시되지 않은 미묘한 부분이있을 수 있습니다. Https://sscce.org – hyde
또한 표시된 코드 조각은 초기화되지 않은 포인터를 derefererences, 일반적으로 즉시 충돌합니다. – hyde
그래, 모든 경고를 제거했을 때 컴파일러 경고를 켜고 돌아온다;) –