unable to read data into the binary tree struct in c

32 views Asked by At

i am programing a binary tree in c with different types of data on it, (im not used to programing in c but the teacher asked us to do it in that language) i made several checkpoints to check where does the program arrives and where the problem lies, ive found that it can read up to checkpoint 3 (spotting the correct binary tree node) with no issues but cant go to checkpoint 4 (storing the values on the node) it just closes imediatly, if anyone knows what is causing this trouble please help me method that the program arrives to:

produto * inserirelemento(produto *head, int codigo, int estoque, char nome[], char marca[], char categoria[], float preco){
    if(head != NULL){
        if(codigo < head->codigo){
            head->esq = inserirelemento(head->esq, codigo, estoque, nome, marca, categoria, preco);
            return head;
        }else if(codigo > head->codigo){
            head->dir = inserirelemento(head->esq, codigo, estoque, nome, marca, categoria, preco);
            return head;
        }else{
            return head;
        }
    }else{
        printf("checkpoint 3\n");
        head = (struct produto *) malloc(sizeof(struct produto));
        head = novoproduto(head, codigo, estoque, nome, marca, categoria, preco);
        return head;
    }
}

the one that its not arriving to:

produto * novoproduto(produto * head, int codigo, int estoque, char *nome, char *marca, char *categoria, float preco){
    head->codigo = codigo;
    head->estoque = estoque;
    strcpy(head->nome, nome);
    strcpy(head->marca, marca);
    strcpy(head->categoria, categoria);
    head->preco = preco;
    printf("checkpoint 4\n");
    return head;
}

this one calls the function (and will implement other functions later)

produto * inserir(produto *head, int codigo, int estoque, char nome[], char marca[], char categoria[], float preco){
        printf("checkpoint 2\n");
    head = inserirelemento(head, codigo, estoque, nome, marca, categoria, preco);
    return head;
}

this one read the inputs and passes to the other functions

produto * inserirnovo(produto *head){
    int codigo, estoque;
    char nome[MAX], marca[MAX], categoria[MAX];
    float preco;
    printf("insira um codigo pro produto\n");
    scanf("%d", &codigo);
    printf("insira um nome pro produto\n");
    scanf("%s", nome);
    printf("insira uma marca pro produto\n");
    scanf("%s", marca);
    printf("insira uma categoria pro produto\n");
    scanf("%s", categoria);
    printf("insira um estoque pro produto\n");
    scanf("%d", &estoque);
    printf("insira um preco pro produto\n");
    scanf("%f", &preco);
    printf("checkpoint 1\n");
    return head = inserir(head, codigo, estoque, nome, marca, categoria, preco);
}

the main:

int main(){

    produto * head;
    livres * lista = NULL;
    head = inserirnovo(head);
}

the struct that its using:

typedef struct produto produto;

struct produto{
    int codigo, estoque;
    char *nome, *marca, *categoria;
    float preco;
    produto *dir, *esq;
};

output:

checkpoint 1
checkpoint 2
checkpoint 3

Process returned -1073741571 (0xC00000FD) execution time : 8.953 s
Press any key to continue.

IDE used: codeblocks language: C

i tried tweaking the string methods sometimes but to no avail i tried looking for similar problems on this website but none helped me (yes i am aware that the code is long and i should provide a minimal reproducible example. but i am not exactly sure to where the problem is)

1

There are 1 answers

1
Davi Franke On

ok so i already solved it, i looked up problem -1073741571 online and found out that it was the struct string size, so i changed the struct to

struct produto{
    int codigo, estoque;
    char nome[MAX], marca[MAX], categoria[MAX];
    float preco;
    produto *dir, *esq;
};