数组初始化为:

char** aldos = NULL;
char** aldoFilenames = NULL;


函数定义为:

int readFilesFromDirectory(char*** dest, char*** nameDest)


通过以下方式传递给函数:

readFilesFromDirectory(&aldos, &aldoFilenames);


计算完文件后,将初始化dest和nameDest:

*dest = (char**)malloc(sizeof(char*)*count);
*nameDest = (char**)malloc(sizeof(char*)*count);
count = 0; //resetting to read in the files again


读取nameDest的第一个文件名,如下所示:

*nameDest[count] = (char*) malloc(sizeof(char)*strlen(findData.cFileName) + 1);
strcpy(*nameDest[count], findData.cFileName);
//can confirm in my program, the value exists properly in *nameDest[count]
count++;


问题出现的地方,当我将其放入循环中时会崩溃(没有真正有用的错误代码):

while (FindNextFile(hfind, &findData) != 0)
{
  *nameDest[count] = (char*) malloc(sizeof(char)*strlen(findData.cFileName) + 1); //doesnt make it past here, CRASH
   sprintf(*nameDest[count],"%s\0",findData.cFileName);
   count++;
 }


任何见解将不胜感激,如果需要,我将尽快添加更多信息

最佳答案

*nameDest[count]the indexing operator place before the dereference operator中,使代码等效于*(nameDest[count]),这不是您想要的,因为nameDest指向数组。您需要使用括号在数组索引之前进行指针取消引用:(*nameDest)[count]

我还应该注意,对操作系统两次轮询目录列表(一次用于计数,一次用于实际名称)是不可靠的,因为在两次轮询之间,计数可能已更改。当发现更多条目时,请考虑使用realloc调整数组的大小。

09-15 11:27