null 终止字符串数组
null terminate an array of strings
我想弄清楚如何让我的字符串数组从 get_arguments
到 NULL
终止,或者如果这不是我的 execv
调用中的问题.
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;
}
我正在尝试读取一个字符串,space 分隔开,类似于 find ./ -name *.h
。我正在尝试将它们输入 execv
.
char (* arguments)[16] = (char **) malloc(256 * sizeof(char));
//...无数行不相关的代码
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);
}
当我在 execv
调用 para
中交换 arguments
时,它按预期工作,但尝试调用 arguments
returns exec failed: Bad address
.如果我从 para
中删除 NULL
,我会遇到同样的问题。我试过 strcpy(temp, (char *) NULL)
,你看到的版本在 get_arguments
中被注释掉了,还有一些其他的东西我记不全了,我的程序范围从 Segmentation fault
到尝试编译失败 strcpy
NULL.
将参数和 temp 的声明更改为 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`.
你想要这个:
char* temp[256]; // an array of 256 char*'s
char * token = strtok(copy, " ");
temp[0] = strdup(token);
int i = 1;
while (token && (token = strtok(NULL, " "))) {
temp[i] = strdup(token);
i++;
}
temp[i] = NULL;
我想弄清楚如何让我的字符串数组从 get_arguments
到 NULL
终止,或者如果这不是我的 execv
调用中的问题.
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;
}
我正在尝试读取一个字符串,space 分隔开,类似于 find ./ -name *.h
。我正在尝试将它们输入 execv
.
char (* arguments)[16] = (char **) malloc(256 * sizeof(char));
//...无数行不相关的代码
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);
}
当我在 execv
调用 para
中交换 arguments
时,它按预期工作,但尝试调用 arguments
returns exec failed: Bad address
.如果我从 para
中删除 NULL
,我会遇到同样的问题。我试过 strcpy(temp, (char *) NULL)
,你看到的版本在 get_arguments
中被注释掉了,还有一些其他的东西我记不全了,我的程序范围从 Segmentation fault
到尝试编译失败 strcpy
NULL.
将参数和 temp 的声明更改为 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`.
你想要这个:
char* temp[256]; // an array of 256 char*'s
char * token = strtok(copy, " ");
temp[0] = strdup(token);
int i = 1;
while (token && (token = strtok(NULL, " "))) {
temp[i] = strdup(token);
i++;
}
temp[i] = NULL;