Question

I've made a binary tree where there are essentially three levels of structs:

typedef struct l {
  char n[15];
  struct l *next;
} List;

typedef struct {
  char rname[20];
  char lname[20];
  List number;
} info;

typedef struct tree {
  info thisnode;
  struct tree *left;
  struct tree *right;
} Tree;

and on this line of code

 t->thisnode.number = t->thisnode.number.next;

I get the error mentioned in the title

Normally with recursive data structures these type of assignments work, despite next having type struct *l. Could anyone help me work out why in this case it isn't working?

Also I would instinctively have the above line as

 t->thisnode.number = t->thisnode.number->next;

But my compiler seems to like that even less.

Was it helpful?

Solution

Well, the types surely are incompatible.

The type of the next field in number is struct l *, but the type of number in info is List, which is an alias for struct l, i.e. the entire struct, not just a pointer to it. So you're trying to assign a pointer into an entire structure, which won't fly.

It seems you're missing a pointer level, perhaps the number field in info should be List *?

OTHER TIPS

The expression t->thisnode.number is of type List (i.e. struct l) while t->thisnode.number->next is of type struct l * (well, would be if it actually compiled as you access a non-pointer member with the pointer access syntax).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top