嗨,我正在为我的一门课做一些事情。我
试图接收这个字符串数组,将其分解为 2 个数组并返回给调用者。我是 C 新手,所以如果你能帮忙,非常有帮助,谢谢……现在我收到编译警告,然后在我运行它时出现Segfault。
我一直在尝试很多事情,例如通过添加 * 和传递 &.. 来更改方法签名,但它似乎不起作用..
我做错了一堆东西...请帮助..我想要的是用 pList[0] = "less" 将 2 个数组返回给 main()
和 cList[0] = "ls" 和 cList[1] = "-t" ...从函数通过引用传入..
继承人我的代码到目前为止
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
void makeLists(char **args, char*** parentList, char*** childList, int *childEnd, int *parentEnd) {
int i;
int foundPipeAt = 0;
int total = 0;
for(i = 0; i < 4; i++) {
total++;
if (strcmp(args[i], "|") == 0) {
foundPipeAt = i;
}
}
*parentList = malloc((foundPipeAt-1) * sizeof(char*));
*childList = malloc((total-(foundPipeAt)) * sizeof(char*));
printf("foundPipe %d\n", foundPipeAt);
for (i = 0 ; i < foundPipeAt ; i++ ) {
*parentList[i] = (char *) malloc( (strlen(args[i])+1) * sizeof(char));
*parentList[i] = args[i];
printf("Parent List: %s\n", *parentList[i]);
}
// Set values for Parent end
*parentEnd = foundPipeAt-1;
*childEnd = total-foundPipeAt;
int k=0;
for (i = foundPipeAt+1 ; i < total ; i++ ) {
*childList[k] = malloc((strlen(args[i])+1) * sizeof(char));
*childList[k] = args[i];
// This prints correctly...
printf("Child List: %s\n", *childList[k]);
k++;
}
}
main() {
int i;
char *args[4];
args[0] = "ls";
args[1] = "-t";
args[2] = "|";
args[3] = "less";
char **pList;
char **cList;
int parentEnd, childEnd;
makeLists(args, &pList, &cList, &childEnd, &parentEnd);
}