1

i'm trying to implement tree structure in c:

this part is from the header file:

typedef struct SP_Tree_Node
{
    char * value;
    struct Node * children;
    int indexOfLastChild;
} Node;

typedef struct SP_Tree
{
    Node root;
} Tree;

when i'm trying to insert new Node into children array the next error appears: "dereferencing pointer to incomplete type" this is the code: (tree's type is Tree *)

Node * newNode = (Node*) malloc(sizeof(Node*));
tree->root.children[tree->root.indexOfLastChild] = newNode;

what am i doing wrong? thank you!!

ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
diana
  • 59
  • 1
  • 4
  • 1
    They say [you shouldn't cast the result of `malloc()` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – MikeCAT Dec 06 '15 at 14:50
  • 1
    `struct Node` and `Node` are unrelated until you say `typedef struct Node Node;`. – n. m. could be an AI Dec 06 '15 at 14:52
  • 1
    `struct Node * children;` -- there is no `struct Node` defined. It should be `struct SP_Tree_Node * children`. And you should `malloc(sizeof(Node))`. – axiac Dec 06 '15 at 14:53
  • @axiac you cannot use `Node * children;` because `Node` isn't defined there yet. – MikeCAT Dec 06 '15 at 14:54
  • @n.m.: That will not work. He needs `struct SP_Tree_Node`. `Node` is in a different namespace. And he **does** typedef `Node` – too honest for this site Dec 06 '15 at 14:54
  • @MikeCAT I deleted that part from my comment. I didn't program in C in the last 6 years and some of the memories started to fade out. Thank you. – axiac Dec 06 '15 at 14:57
  • 1
    `Node * newNode = (Node*) malloc(sizeof(Node*));` doesn't seem good. I think it should be `Node * newNode = malloc(sizeof(Node));` (allocate sizeof `Node` (the struct) instead of `Node*` (a pointer)) – MikeCAT Dec 06 '15 at 14:59
  • @Olaf `typedef struct Node Node;` workls jolly well. Unless `typedef somethingelse Node;` already exists in the same scope, which is probably a mistake and should be removed or renamed in order to keep simple things simple. – n. m. could be an AI Dec 06 '15 at 15:02
  • @n.m.: See the text. He already does `typedef` `Node`. He just has to use `SP_Tree_Node` instead of `Node` for `children`. There is no `struct Node` Whether one uses the same ident for the struct-tag depends on the coding standard used. I generally recommend a constant suffix or prefix. But that is a personal preference. – too honest for this site Dec 06 '15 at 15:11
  • @Olaf no, he needs to (expletive removed) ditch the SP_Tree_Node identifier and just define `struct Node` instead. Simple. Tells the story. The tag already has the constant prefix, it's spelled `struct`. – n. m. could be an AI Dec 06 '15 at 15:16
  • @n.m.: That is a matter of personal preference! Leave that to each implementor. Many coding standards require to use a disctint name, e.g. with a suffix like `_s` or so to differentiate the names. – too honest for this site Dec 06 '15 at 15:19
  • Obviously such a coding standard is not in effect here ;) If a coding standard requires you to add _s or whatever, add it, but IMO it's just noise. You already know it's an "_s", you have this nice coloured word `struct` right before it! – n. m. could be an AI Dec 06 '15 at 15:26

1 Answers1

3

struct Node is different from Node, and it doesn't seem you defined `struct Node.

Try changing struct Node * children; in the declaration of struct SP_Tree_Node to struct SP_Tree_Node * children;

UPDATE:

You use array of Node*, so the declaration should be struct SP_Tree_Node ** children; (add one more asterisk).

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • @this An pointer to `Node` is assigned to an element of `tree->root.children` in the code in the question, so `tree->root.children` should be (a pointer pointing at the first element of) an array of `Node*`. – MikeCAT Dec 06 '15 at 23:08