0

I'm tackling load and decided to make a create_node function for allocating memory, NULLing all children and setting is_word as false.

What I wrote is essentially the same as in this question

struct node create_node(void)
{
    node *n = malloc(sizeof(node));
    if (n == NULL)
    {
        return n;
    }
    n->is_word = false;
    for (int x = 0; x < N; x++)
    {
        n->children[x] = NULL;
    }
    return n;
}

However I can't declare a new node outside of that function, either getting a

error: incompatible pointer types initializing 'node *' (aka 'struct node *') with an expression of type 'node (void)' (aka 'struct node (void)')

node *new_node = create_node;

or

error: expression is not assignable

for

node *new_node;
&new_node = create_node;

Since from the answered question I understand that I'm getting an address back.

I know that it's a bad way of figuring things out, but I have tried all variations of *, & and nothing , declaring an empty new node before trying to assign it to the function and still stuck.

1

I think I got it.

So the first line of the function should have been this:

struct node *create_node(void)

I the * has nothing to do with the name of the function but rather with the fact that create_node should return a pointer to my struct, not the struct itself.

And in the main part it should have been:

node *n = create_node();

Declaring a pointer to a node and initializing it to another pointer of a node, created (malloced) within the function.

I hope I am in the right. Please correct me if I am not.

1
  • " Declaring a pointer to a node and initializing it to another pointer of a node, created (malloced) within the function. " This statement helped me very much to understand better. Thank you very much ! @Talim Aug 2 at 22:04

You must log in to answer this question.

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