get_arguments
에서 NULL
으로 문자열 배열을 가져 오는 방법을 알아 내려고하고 있습니다. 그렇지 않으면 내 execv
호출에서 문제가되지 않습니다.null 문자열 배열을 끝내기
char ** get_arguments(const char * string) {
char * copy = strdup(string);
char * remove_newline = "";
for(;;) {
remove_newline = strpbrk(copy, "\n\t");
if (remove_newline) {
strcpy(remove_newline, "");
}
else {
break;
}
}
char (* temp)[16] = (char *) malloc(256 * sizeof(char));
char * token = strtok(copy, " ");
strcpy(temp[0], token);
int i = 1;
while (token && (token = strtok(NULL, " "))) {
strcpy(temp[i], token);
i++;
}
char * new_null;
//new_null = NULL;
//strcpy(temp[i], new_null);
if(!temp[i]) printf("yup\n");
int c = 0;
for (; c <= i; c++) {
printf("%s ", temp[c]);
}
return temp;
}
find ./ -name *.h
과 비슷한 문자열로 읽으려고합니다. execv
에 입력하려고합니다. 관련이없는 코드
pid = fork();
if (pid == 0) {
arguments = get_arguments(input_string);
char * para[] = {"find", "./","-name", "*.h", NULL};
execv("/usr/bin/find", (char * const *) arguments);
//printf("%s\n", arguments[0]);
printf("\nexec failed: %s\n", strerror(errno)); //ls -l -R
exit(-1);
}
의
char (* arguments)[16] = (char **) malloc(256 * sizeof(char));
//...numerous 라인은 내가 의도하지만 arguments
반환 exec failed: Bad address
로 전화를하려고대로 작동 para
의 execv
호출 arguments
를 교환합니다. para
에서 NULL
을 제거하면 동일한 문제가 발생합니다. 나는 strcpy(temp, (char *) NULL)
을 보았습니다. 버전은 get_arguments
이며, 전체 내용을 기억할 수없는 많은 것들이 있습니다. Segmentation fault
에서 strcpy
으로 컴파일하는 데 실패했습니다.
인자와 임시 변수의 선언을 char ** arguments = (char *) malloc(256 * sizeof(char));
으로 변경합니다.``char ** temp = (char *) malloc (256 * sizeof (char)); clears up
경고 : 호환되지 않는 포인터 유형 but causes segfault on all calls to
get_arguments`에서 초기화.
내가 가장 혼란 스러워요 내가 선언 된 배열 공간에서 사용되지 않은 공간이 null 디폴트로 말할 읽어 대부분의 장소,하지만 실패 때문에'경우 printf ("yup \ n");'시도한 모든 조건 하에서 테스트. – Kebtiz
'malloc()'앞에'(char *)'캐스트를 제거 할 수 있습니다. –
감사 : 경고 : '호환되지 않는 포인터 타입으로 초기화'경고. 이상하게도 반복적으로 사용되는 컨벤션입니다. 명시적인 유형이 필요한 상황이 있습니까? – Kebtiz