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). 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 .....).
However, a struct
type can contain a pointer
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). 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
.
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").
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.
But be aware that with your concept each folder can only contain 10 subfolders at most.
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. `
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.