0

For a project, I have to insert nodes in a tree. This tree is a big one as every node can have at maximum 26 children. In order to do so I've created a structure which is the following :

typedef struct s_node
{
    char letter;
    struct s_node* f_letters[26];
    word* flechies;
}t_node;

typedef struct s_tree
{
    t_node* root;
}t_tree;

I'm also having this structure to affect the word from the .txt file :

typedef struct
{
    char content[100];
    char base_word[100];
    int nature;
    int genre;
    int nombre;
    int temps;
    int personne;
}word;

The letter store one letter and the f_letters store pointers to the next character. The letter are coming from a .txt file that looks like this :

stabilimetre    stabilimetre   Nom:Mas+SG

I'm trying to put the letter from the first word (stabilimetre here) in nodes (one letter per node). I've created two functions that I use to create node and display the tree :

t_node* new_value(char letter,t_tree *Tree, t_node* leaf)
{
    t_node* n_node;
    t_node n_node1;
    n_node = &n_node1;
    leaf->f_letters[0] = n_node;
    n_node->letter = letter;
    for (int i=0; i<26; i++)
    {
        n_node->f_letters[i] = NULL;
    }
    return n_node;
}

void display_tree(t_tree t)
{
    printf("The letter in this node is %c.\n",t.root->letter);
    t_node* temp;
    temp = t.root->f_letters[0];
    printf("The letter in this node is %c.\n",temp->letter);
    while(temp->f_letters[0] != NULL)
    {
        temp = temp->f_letters[0];
        printf("The letter in this node is %c.\n",temp->letter);
    }
}

This is my main :

int main () {
    FILE *fp;
    char str[5000];
    char type[100];

    t_tree Letter;
    t_node* temp;
    t_node temp1, first;

    temp = &temp1;
    Letter.root=NULL;

    fp = fopen("dico_reducted.txt" , "r"); // Open the file in read mode "r"

    if(fp == NULL) {
        perror("Error opening file");
        return(-1); // test if the file is not empty or if there is any issue
    }

    while (fgets (str, sizeof(str), fp)!=NULL) {
        word A;
        char word;
        sscanf(str,"%s\t%s\t%s",&A.content, &A.base_word, &type); //Put every information from the text in the right spot
        Letter.root = &first;
        first.letter = 'N'; //This letter represent the type of the word (here noun)
        first.f_letters[0] = temp;
        temp->letter = A.content[0]; //The first 2 letters are putted manually and this is working

        for (int i=1; i<12; i++)
        {
            temp = new_value(A.content[i], &Letter, temp); // Here I put the following letters inside the tree
        }
    }

    display_tree(Letter); //Here I'm trying to display the whole tree (It is working for only the first 2 letters
    fclose(fp); // Close the file

    return 0;
}

The main issue is that I don't knwo which function is not working. The output I have for now is :

The letter in this node is N. //This is good
The letter in this node is s. //This is good
The letter in this node is ▲. //Why is it displaying kind of an adress and why the program is stopping ?

I've tried many things like changing part of the functions or do the display manually by some printf without using the function but the same error occured. I think that the main issue is the new_value function but I don't get were it is coming from. The final output should be :

The letter in this node is N. 
The letter in this node is s. 
The letter in this node is t.
The letter in this node is a.
The letter in this node is b.
The letter in this node is i.
The letter in this node is l.
The letter in this node is i.
The letter in this node is m.
The letter in this node is e.
The letter in this node is t.
The letter in this node is r.
The letter in this node is e.
  • How are `word` and `type_def` defined? Please supply a complete code example, and make sure it builds without warnings. I'm reluctant to investigate unexpected behavior when there is still potential _undefined_ behavior. Like a call to `sscanf` with too few parameters. – Ruud Helderman Nov 06 '22 at 13:00
  • Function `new_value` is doing something very wrong: it allocates a node on the stack (local variable `n_node1`) and returns a pointer to that node. Instead, you must allocate that node on the heap. – Ruud Helderman Nov 06 '22 at 13:13
  • @RuudHelderman Actually type_def is a function that put every information in the right spot in the word structure but those informations are not used in the functions that are not working. I can provide it but it is not relevent in this case – Antoine Dupont Nov 06 '22 at 13:15
  • @RuudHelderman When you mean the heap is it like using malloc ? – Antoine Dupont Nov 06 '22 at 13:16
  • @RuudHelderman What do you mean by too few arguments for sscanf ? – Antoine Dupont Nov 06 '22 at 13:23
  • Answers: (1) Please supply code that is __complete__. It helps us build your code and reproduce your issue on our own development machines. (2) Correct, `malloc`. (3) Please compile with all warnings enabled! Clang reports this warning that is actually a serious memory overrun: "warning: more '%' conversions than data arguments." Notice there are 4 `%s` in the format specifier, and only 3 parameters that follow it. – Ruud Helderman Nov 06 '22 at 13:38
  • @RuudHelderman I've removed the typedef() function and added the structure word. I don't know how to enable all warning... I really appreciate your help, thank you very much ! – Antoine Dupont Nov 06 '22 at 13:52
  • Thanks for the edit, that helps. You are suffering from a [segfault](https://stackoverflow.com/questions/2346806/what-is-a-segmentation-fault). Replacing `n_node = &n_node1;` with `n_node = malloc(sizeof(t_node));` seems to fix it. – Ruud Helderman Nov 06 '22 at 14:14
  • How to enable all warnings: that depends on your compiler. Use google. I found recommendations for GCC [here](https://stackoverflow.com/questions/3375697/what-are-the-useful-gcc-flags-for-c). – Ruud Helderman Nov 06 '22 at 14:29
  • @RuudHelderman thank you very much it is finally working now ! You made my day !!! – Antoine Dupont Nov 06 '22 at 14:54

1 Answers1

0

Replacing n_node = &n_node1; with n_node = malloc(sizeof(t_node));
solve my issue.

James Risner
  • 5,451
  • 11
  • 25
  • 47