Nested linked lists in C -


i'm trying implement nested linked list in c, used hierarchical menu. however, gcc (v4.9.3-1) complaining nested structures, , have no idea how fix this. here minimum (non)working example.

is nesting possible in c?

main.c

#include "menu.h"  int main(void) {     init_menu();     return 0; } 

menu.c

#include "menu.h"  menuitem_t lvl_0_mainmenu = {     .size = 0, };  menuitem_t lvl_1_measurements = {     .size = 0, };  void init_menu(void) {     menu_add_child(&lvl_0_mainmenu, &lvl_1_measurements); }  void menu_add_child(menuitem_t *parent, menuitem_t *child) {     parent->children[parent->size] = child;     child->parent = parent;     parent->size++; } 

menu.h

typedef struct {     unsigned char size;     menuitem_t children[10];     menuitem_t *parent; } menuitem_t;  extern menuitem_t lvl_0_mainmenu; extern menuitem_t lvl_1_measurements;  void init_menu(void); void menu_add_child(menuitem_t *parent, menuitem_t *child); 

based on answers @bolov , @sps (once again, both of them), here minimum working example:

main.c

#include "menu.h"  int main(void) {     init_menu();     return 0; } 

menu.c

#include "menu.h"  menuitem_t lvl_0_mainmenu = {     .size = 0, };  menuitem_t lvl_1_measurements = {     .size = 0, };  void init_menu(void) {     menu_add_child(&lvl_0_mainmenu, &lvl_1_measurements); }  void menu_add_child(menuitem_t *parent, menuitem_t *child) {     parent->children[parent->size] = child;     child->parent = parent;     parent->size++; } 

menu.h

struct menuitem_t {     unsigned char size;     struct menuitem_t *children[10];     struct menuitem_t *parent; };  typedef struct menuitem_t menuitem_t;  extern menuitem_t lvl_0_mainmenu; extern menuitem_t lvl_1_measurements;  void init_menu(void); void menu_add_child(menuitem_t *parent, menuitem_t *child); 

the difference between corrected program , original (non)working program, children array defined array of pointers variables of type menuitem_t instead of array of variables of same type. other difference nested list (inside structure) should contain keyword struct @bolov explained.

you need use struct type used inside itself, if typedef later on.

e.g. won't work:

struct x_ {   x* next; };  typedef struct x_ x; 

but will

struct x_ {   struct x_* next; }; 

as side note, don't form:

typedef struct { } x; 

i use:

struct x { }; typedef struct x x; 

but maybe me being more fond of c++.

if want use form, it's same: need add struct , works:

typedef struct {   struct x2* next; } x2; 

regarding:

struct x {    struct x arr[10]; }; 

you can't have that! array in our way understand why. let's simplify:

struct x {    int a;    struct x var; }; 

this can't be. size x be? sizeof(x) = sizeof(int) + sizeof(x) + padding. see problem? can have pointer x, not object x inside x.

returning array. need dynamic arrays:

struct x {    struct x* arr;    int arr_size; }; 

it gets more complicated need manage memory (malloc/free fun), can't avoid it.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -