结构数组的值突然改变(?)

时间:2014-12-20 21:45:20

标签: c arrays struct

所以我一直在寻找这段代码几个小时,我似乎无法理解发生了什么,尽管我努力用printf调试这个代码到处都是。以下功能是我的主要问题所在:

void drawFolders() {

printf("Going to call getSubFolder\n");
getSubFolders(".");

printf("DEBUG 1 - Return from getFolderName is: %s\n", getFolderName(3));

printf("DEBUG 2 - Return from getFolderName is: %s\n", getFolderName(5));

int k;

for(k = 0; k < getNumberFolders(); k++) {
    printf("DEBUG INLOOP %d - Return from getFolderName is: %s\n",k, getFolderName(k));


    draw_sprite(k*60 + 30, 50, folder);

    //draw_string(temp[i].name,i*60 + 30, 50);

}

}

基本上,在DEBUG1和DEBUG2中,我的函数getFolderName()似乎工作正常,即返回一个没有问题的字符串。但是,当我尝试在for循环中调用此函数时,我从getFolderName获取的所有东西都是垃圾(除了第一个),即使k是3或5(我是这样的情况)在for循环之前测试并且工作正常)。 k也没错,因为我也打印它。 getFolderName对数组没有任何作用,这里是函数:

char* getFolderName(int index) {
printf("Values inside getFolderName() function : %s\n", currentFolders[index].name);
return currentFolders[index].name ;

}

显示currentFolders是非常重要的:

typedef struct Directories {

    char* name;
    int active;

} Directory;

Directory currentFolders[30];

输出

Dec 20 22:18:48 192 kernel: Vai chamar o getSubFolder
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: .
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: ..
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: usr
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: lab4
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: proj
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: bin
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: boot
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: dev
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: etc
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: home
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: mnt
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: root
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: sbin
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: tmp
Dec 20 22:18:48 192 kernel: Values inside getSubFolders function: var
Dec 20 22:18:48 192 kernel: DEBUG 0Values inside getFolderName() function : lab4
Dec 20 22:18:48 192 kernel: DEBUG 1 - Return from getFolderName is: lab4
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : bin
Dec 20 22:18:48 192 kernel: DEBUG 2 - Return from getFolderName is: bin
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : .
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 0 - Return from getFolderName is: .
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : 
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 1 - Return from getFolderName is: 
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : 
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 2 - Return from getFolderName is: 
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : 
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 3 - Return from getFolderName is: 
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : 
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 4 - Return from getFolderName is: 
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : 
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 5 - Return from getFolderName is: 
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : 
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 6 - Return from getFolderName is: 
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : 
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 7 - Return from getFolderName is: 
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : 
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 8 - Return from getFolderName is: 
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : 
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 9 - Return from getFolderName is: 
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : 
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 10 - Return from getFolderName is: 
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : 
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 11 - Return from getFolderName is: 
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : 
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 12 - Return from getFolderName is: 
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : 
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 13 - Return from getFolderName is: 
Dec 20 22:18:48 192 kernel: Values inside getFolderName() function : 
Dec 20 22:18:48 192 kernel: DEBUG INLOOP 14 - Return from getFolderName is: 

我认为其他被称为问题的功能与此问题无关,但你可能想看一下,所以无论如何它都是:

  int getSubFolders(char* foldername) {
    num_folders = 0;

    struct dirent *de=NULL;
      DIR *d=NULL;

      d=opendir(foldername);
      if(d == NULL) {
        perror("Couldn't open directory");
        return(2);
      }

      int i = 0;
      // Loop while not NULL
      while(de = readdir(d)) {
        num_folders++;
        Directory temp;
        currentFolders[i].name = de->d_name;

        strcpy( currentFolders[i].name, de->d_name);

//      printf("nome %s\n",de->d_name);

        //printf("temp.name %s\n",temp.name);

      //  currentFolders[i] = temp;

        printf("Values inside getSubFolders function: %s\n",currentFolders[i].name);

        i++;
      }

      printCurrentFolders();

      closedir(d);
      return(0);

}

1 个答案:

答案 0 :(得分:0)

以这种方式修复,结构声明它就像这个

typedef struct Directories {
    char name[256];
    int active;
} Directory;

并删除此行

currentFolders[i].name = de->d_name;

问题是由closedir(d);清理opendir使用的资源造成的,struct dirent *是这些资源的一部分。