0

EDIT:

I've changed my struct to match what's in the video as thus in my header file:

typedef struct{
    bool end;
    struct node* child[27];
}node;

And in my C file when I do:

if(trav->child[cIndex]!=0){
                printf("Child node is empty\n");
                trav->child[cIndex] = trav;

I get:

dictionary.c:84:41: error: incompatible pointer types assigning to 'struct node *'
 from 'node *' [-Werror,-Wincompatible-pointer-types]
                trav->child[cIndex] = trav;

Node* trav is malloced properly just liket he root node. Why is the compiler thinking one's a "node*" and the other is "struct node*"?

Also, how do you initialize the values of a node? I did:

node* root;
root = malloc(sizeof(node));
root = 0;

Which, I hope initializes the boolean and the elements of the child node array to 0. Is this a correct assumption to make?

-----------END EDIT----------------

I am using the trie method and have the usual typedef here:

typedef struct{
    bool end;
    struct node* child[27];
}node;

But when I do

struct node* root;
root = (struct node *) malloc(sizeof(node));
root->end = false;

I am getting:

dictionary.c:47:9: error: incomplete definition of type 'struct node'
root->end = false;

Any ideas would be appreciated.

2 Answers 2

1

The definition of struct node is incomplete in struct node itself.

To fix this, you need to define struct node like this:

typedef struct node{
    bool end;
    struct node* child[27];
}node;

Now, child will know what it is. :)

You need to do typedef struct name { /* struct */ } name; whenever you have a self-referencing struct.

0

Are you trying to define a new struct? The command struct node* root; is an attempt to do so. You've previously defined a struct with the typedef command.

Instead, just remove the word struct and do this:

 node* root;
 root = malloc(sizeof(node));

This creates a variable called root that is a pointer to a node structure.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • If I remove all "struct" keyword I get things like this: dictionary.c:81:41: error: incompatible pointer types assigning to 'struct node ' from 'node *' [-Werror,-Wincompatible-pointer-types] trav->child[cIndex] = trav; Instead what I did was: struct node{ bool end; struct node child[27]; }; And added "struct" keyword to all the mallocs and declarations. I would still like to know why one works and the other doesn't though.
    – user10805
    Commented Feb 24, 2016 at 1:40
  • the implicit casting (inside the parentheses) before the malloc is not necessary. The system knows that you are allocating memory for the struct and handles it.
    – Cliff B
    Commented Feb 24, 2016 at 1:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .