0
很容易看到在这里试图完成什么。读取文件列表并将其传递给另一个函数。 这是行不通的。 当我尝试将文件名存储在本地char **中时,它工作得很好,但无法通过指针将其发回。出现分段错误。无法通过char ***作为参数
int main(){
char** fileList;
int noOfFiles;
char* path = ".";
makeList(&fileList, &noOfFiles, path);
return 0;
}
void makeList(char ***fileList, int* noOfFiles, char* path){
struct dirent **fileListTemp;
*noOfFiles = scandir(path, &fileListTemp, NULL, alphasort);
int i;
fileList = malloc(sizeof(char***));
*fileList = malloc(sizeof(char**));
printf("total: %d files",*noOfFiles);
for(i = 0; i < *noOfFiles; i++){
printf("%s\n",fileListTemp[i] -> d_name); //works just fine
}
*fileList = malloc(*noOfFiles * sizeof(char*));
for(i=0; i < *noOfFiles; i++){
//*fileList[i] = fileListTemp[i] -> d_name; this didn't work either...
strcpy(*fileList[i], fileListTemp[i]->d_name);
printf("%s\n", *fileList[i]);
}
//fileList = &list;
return;
}
分段错误总是... 并没有从主()
删除参数'int aFlag'或将其添加到调用中。指针'&fileList'在函数调用后不存在,因此分配给它并不是一个好主意。 – dastrobu
指定它不是一个好主意?将fileList存储在本地char **中,并将它的指针返回到main working。但我不明白为什么...... – kBisla