简体   繁体   English

声明一个包含指向该类型结构的指针数组的结构

[英]Declaring a struct containing an array of pointers to that type of struct

I am trying to program a file manager and would like to use folders containing an array of subfolders. 我正在尝试对文件管理器进行编程,并且想使用包含子文件夹数组的文件夹。

I was thinking of something like 我在想类似的东西

struct folder {
 char* name;
 struct folder subfolders [10];
}

Is that possbile in any way? 有可能吗?

No, it is not possible for a struct type to contain an array of the same type (since the type would generally be diagnosed as incomplete). 不, struct类型不可能包含相同类型的数组(因为通常会将该类型诊断为不完整)。 Furthermore, if every variable of that type contained one or more members of the same type, the type definition would be infinitely recursive (an A containing an A containing an A .....). 此外,如果该类型的每个变量都包含一个或多个相同类型的成员,则类型定义将是无限递归的(A包含A,而A包含A .....)。

However, a struct type can contain a pointer 但是, struct类型可以包含一个指针

struct folder1
{
    char* name;
    struct folder1 *a_subfolder;
};

or an array of pointers 或指针数组

struct folder2
{
    char* name;
    struct folder2 *subfolders [10];
};

In both cases, code which uses theses types need to take care to ensure the pointers are initialised correctly, and that they are worked with sensibly (eg not trying to access the value of an uninitialised pointer, not dereferencing NULL, etc). 在这两种情况下,使用这些类型的代码都必须注意确保正确初始化了指针,并确保它们可以合理地工作(例如,不尝试访问未初始化的指针的值,不取消引用NULL等)。 Read up on pointers before using this sort of construct - getting things wrong with pointers often means a program that crashes in gruesome ways. 在使用这种构造之前,请先阅读指针-指针出错会导致程序崩溃。

No, you cannot do this, you need to do this: 不,您不能这样做,您需要这样做:

struct folder {
 char* name;
 struct folder *subfolders [10];
}

Now a variable of type struct folder contains 10 pointers to variables of type struct folder . 现在, struct folder类型的变量包含10个指向 struct folder类型变量的指针

Before going on, you absolutely need to get familiar with pointers and dynamic memory allocation. 在继续之前,您绝对需要熟悉指针和动态内存分配。

Look into linked lists and binary trees first which use a similar concept. 首先查看使用相似概念的链表和二叉树。 There are tons of examples and tutorials on the web (Google "c linked list example"). 网络上有大量的示例和教程(Google“ c链表示例”)。

Your solution: 您的解决方案:

struct folder {
 char* name;
 struct folder subfolders [10];
}

doesn't make sense, because each struct folder would contain again 10 struct folder , each of which would contain 10 struct folder and so on until infinity. 这是没有意义的,因为每个struct folder将再次包含10 struct folder ,每个struct folder将包含10 struct folder ,依此类推直到无限。

But be aware that with your concept each folder can only contain 10 subfolders at most. 但是请注意,按照您的概念,每个文件夹最多只能包含10个子文件夹。

struct folder {
 char* name;
 struct folder subfolders [10];  // bad
}

Is is not possible that way per @Peter fine answer as struct folder is not fully defined before its complete definition is needed. @Peter的最佳答案是不可能的,因为在需要完整定义之前, struct folder还没有完全定义。 ` `

Is that possible in any way? 有可能吗?

Yes, consider dynamically allocation: 是的,考虑动态分配:

Make functions to create, add a sub-folder, and delete. 使函数创建,添加子文件夹和删除。 Many refinements possible. 许多改进是可能的。

typedef struct folder_s {
  char* name;
  size_t n;
  struct folder_s **subfolder;
} folder;

folder *folder_alloc(const char *name) {
  folder *root = malloc(sizeof *root);
  if (root) {
    root->n = 0;
    root->subfolder = NULL;
    root->name = strdup(name);
    if (root->name) {
      return root;
    }
    free(root);
  }
  return NULL;
}

folder *folder_add_subfolder(folder *f, folder *sf) {
  size_t new_n = f->n + 1;
  void *tmp = realloc(f->subfolder, sizeof *(f->subfolder) * new_n);
  if (tmp == NULL) {
    return NULL;
  }
  f->subfolder = tmp;
  f->subfolder[f->n] = sf;
  f->n = new_n;
  return f;
}

// Only need to free the root folder, recursion will handle the sub-folders.
void folder_free_tree(folder *f) {
  if (f) {
    while (f->n > 0) {
      f->n--;
      folder_free_tree(f->subfolder[f->n]);
    }
    free(f->subfolder);
    f->subfolder = NULL; // Useful for debug
    free(f->name);
    f->name = NULL;  // Useful for debug
    free(f);
  }
}

The short answer is yes, something to that effect would work. 简短的答案是肯定的,可以达到这种效果。 You might want to add a struct folder parentfolder so you can navigate up as well as down. 您可能要添加一个struct folder parentfolder以便可以上下导航。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM