Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当recursivley添加到字符**时,该字符一直被覆盖_C_Arrays_Pointers_Heap Memory_Dynamic Memory Allocation - Fatal编程技术网

当recursivley添加到字符**时,该字符一直被覆盖

当recursivley添加到字符**时,该字符一直被覆盖,c,arrays,pointers,heap-memory,dynamic-memory-allocation,C,Arrays,Pointers,Heap Memory,Dynamic Memory Allocation,我有一段代码,应该把文件放在一个目录中,放入一个字符串数组,然后打印内容。当前在我的Functions.c文件中,它成功地写入了正确的数组索引,但在写入下一个数组索引时被覆盖。感谢您的帮助 Main.c #include "Headers.h" void main(){ listFiles("C:/Users/me/Documents/testDoc"); //printf("%s",fileArray[0]); printf("End

我有一段代码,应该把文件放在一个目录中,放入一个字符串数组,然后打印内容。当前在我的Functions.c文件中,它成功地写入了正确的数组索引,但在写入下一个数组索引时被覆盖。感谢您的帮助

Main.c

#include "Headers.h"


    void main(){
        listFiles("C:/Users/me/Documents/testDoc");
        //printf("%s",fileArray[0]);
        printf("End");
        return;
    }
职能.c

#include "Headers.h"


void listFiles(const char *foldername){
    struct dirent *dp; //creates object of struct dirent
    DIR *dir=opendir(foldername); //creates object of directory
    char **fileArray=(char **)calloc(1,sizeof(char *));
    if (!dir) //checks to see if directory exsists
        return;
    int numloops=1;
    while ((dp = readdir(dir)) != NULL)//recursivley goes through list
    {
        printf("%d\n",numloops);
        if(numloops==1){
            fileArray[0]=dp->d_name; //maps first file to position zero
            printf("%s test\n",fileArray[0]);
        }
        else{
            fileArray=realloc(fileArray,numloops*sizeof(char *));
            fileArray[numloops-1]=dp->d_name; //maps next file in order to position -1 to make enough memory since array starts at 0
            printf("%s test\n",fileArray[numloops-1]);
        }

        numloops++;
        printf("%s original\n", dp->d_name);//maps d_name recursivley
    }
    // Close directory stream
    printf("Value at index 2 %s\n",fileArray[2]);
    printf("Value at index 3 %s\n",fileArray[3]);
    closedir(dir);
    return;
}
标题.h

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
void listFiles(const char *foldername);
预期产量

1
. test
. original
2
.. test
.. original
3
Doc 1.txt test
Doc 1.txt original
4
Doc 2.txt test
Doc 2.txt original
5
Doc 3.txt test
Doc 3.txt original
Value at index 2 Doc 1.txt
Value at index 3 Doc 2.txt
End
发件人:

返回的指针和结构中的指针可能会失效,或者该结构或存储区域可能会被同一目录流上对
readdir()
的后续调用所覆盖

这句话的意思是,
readdir
可以反复返回指向相同结构的指针,这意味着
d_name
成员每次都是相同的

所以当你这么做的时候

fileArray[numloops-1]=dp->d_name;
使数组中的所有元素指向完全相同的内存

解决方案是复制字符串
d_name
,例如

fileArray[numloops-1]=strdup(dp->d_name);
当然,完成后,需要
释放数组中的每个字符串

fileArray[numloops-1]=strdup(dp->d_name);