request for Member 'attributeOuDecisao' in Something not a Structure or Union

Asked

Viewed 380 times

0

Tree initialization error, the problem with the category variables and atributoOuDecisao.

typedef struct node {
    int categoria;
    int atributoOuDecisao;
    struct node *prox;
    struct node *lista;
} No;

No *criaArvore(void){
    No *inicio = (No*)malloc(sizeof(No));
    inicio.atributoOuDecisao = NULL;
    inicio.categoria = NULL;
    inicio->lista = NULL;
    inicio->prox = NULL;
    printf ("inicio criado");
    return inicio;
}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

2

When using a pointer to a structure the correct operator to access the members is always the ->. In addition whole types should be initialized with 0 and not with NULL.

#include <stdio.h>
#include <stdlib.h>
typedef struct node {
    int categoria;
    int atributoOuDecisao;
    struct node *prox;
    struct node *lista;
} No;

No *criaArvore(void){
    No *inicio = malloc(sizeof(No));
    inicio->atributoOuDecisao = 0;
    inicio->categoria = 0;
    inicio->lista = NULL;
    inicio->prox = NULL;
    printf ("inicio criado");
    return inicio;
}

int main() {
    criaArvore();
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.