0

I'm trying to insert true into the second branch of a node.

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>


typedef struct node
{
    bool correct;
    struct node *children[2];

}node;





int main(void)
{
    node *top_of_node;

    top_of_node = malloc(sizeof(node));

    bool answer = top_of_node -> children[0] -> correct = true ;

    printf("true or false (1 or 0 ) = %d\n "),answer;
}

the error message i get is

 error: more '%' conversions than data arguments [-Werror,-Wformat]

can someone show me how to insert True or False into the following branches. That would be of great help.

0

Your question is unclear, but I think that you're trying to set the value of the correct field in a note to true.

First of all, this line doesn't work:

bool answer = top_of_node -> children[0] -> correct = true ;

This is essentially a=b=c; chain assignments are not valid in c.

Now, say that you have a node, called foo, and memory has been allocated to it. To set correct to true, just do this:

foo->correct = true;

Inserting a node into the trie is another story. In your code above, you've created the root node, but then the code is trying to set a value in a child that doesn't exist yet. That will generate an error. You need to insert the child node first.

The following is a revised version of your code that shows how to insert a child in a particular location, children[0], in the root node. It also shows how to print the value of the bool directly without using an intermediate variable.

int main(void)
{
        node *top_of_node;

        top_of_node = malloc(sizeof(node));

        top_of_node->children[0] = malloc(sizeof(node));

        top_of_node -> children[0] -> correct = true ;

        printf("DIRECT true or false (1 or 0 ) = %d\n ",top_of_node -> children[0] -> correct);

        bool answer = top_of_node -> children[0] -> correct;
        printf("true or false (1 or 0 ) = %d\n ",answer);
}

Of course, this doesn't demonstrate how to walk a trie or step from parent to child. You should review the video shorts on trees and tries.

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

2
  • I have checked out the video short on tries, however there isn't one on trees. – Ryan Hinchliffe Sep 19 '19 at 14:17
  • Sure there is. A tree is a collection of linked lists. How do you add to a linked list? – Cliff B Sep 19 '19 at 18:08

You must log in to answer this question.

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