很容易看出这里要完成什么。读取文件列表并将其传递给另一个函数。为什么这不起作用。当我尝试将文件名存储在本地 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;
}
分段错误总是......并且没有希望从 main() 打印 fileList