Doubt over struct and list in c

1

I'm in trouble when I need to give a person value, in this case I need to give the name and age value to the person. I tried to create a struct PESSOA and a list that contains a person and an id, but during compilation it presents / displays the image error to me.

Belowismycode:

typedefstructPESSOA{charnome[100];intidade;}Pessoa;typedefstructNO{intid;Pessoapess;structNo*prox;}No;intmain(){intcontrole;intid=0;No*no_inicial;No*no_proximo;no_inicial=(No*)malloc(sizeof(No));no_proximo=no_inicial;no_proximo->prox=NULL;while(1){printf("Insira o nome");
        gets(no_proximo->pess().nome);
        printf("Insira a idade");
        scanf("%d", &no_proximo->pess->idade);
        no_proximo->id = id;
}
}
    
asked by anonymous 05.08.2017 / 20:47

2 answers

2

So you should compile:

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

typedef struct PESSOA {
    char nome[100];
    int idade;
} Pessoa;

typedef struct NO {
    int id;
    Pessoa pess;
    struct NO *prox;
} No;

int main() {
    int controle;
    int id = 0;
    No *no_inicial;
    No *no_proximo;

    no_inicial = (No *) malloc(sizeof(No));
    no_proximo = no_inicial;
    no_proximo->prox = NULL;

    while(1) {
        printf("Insira o nome");
        fgets(no_proximo->pess.nome, 100, stdin);
        printf("Insira a idade");
        scanf("%d", &no_proximo->pess.idade);
        no_proximo->id = id;
    }
}

I do not know if this program will do what you want though.

Some comments:

  • Never, ever, dare to think about using the gets function. This function is unanimously hated and detested in the C developer community for being poorly designed and extremely insecure. In your code, I replace it with an equivalent but safe call of fgets .

  • Do not confuse No with NO . The name No is what you gave as a nickname for struct NO . The name NO isolated means nothing. It's no use putting No inside the statement of { ... } of own No because this creates an egg and chicken problem: In order for the compiler to know what is No within the structure, it already has to be finished to read the structure. However, when using struct NO , the compiler has already seen the header before, although it has not yet finished reading the structure, so there is no problem.

  • The use of a->b is the same as (*a).b . If what exists in a is a pointer to a structure in which you want to access an element, use -> . If a is not a pointer, and use . . In the case of its No , it has a pointer ( prox ) to another No , and because it is pointer, you access its contents with -> . In its No structure, there is also another nested structure ( pess ) of type Pessoa which is not a pointer and therefore should have its contents accessed with . .

  • Only use () after a name if it represents a function. The use of no_proximo->pess() would only make sense if pess was a pointer to function, something that beginners in the C language will not find so soon. So, do not put parentheses after the name of a field of some struct (be it accessed by -> or . ).

05.08.2017 / 21:11
0

Simplified Version - Tested

Create a file named main.c

#include <stdio.h>
#define EXIT_SUCCESS 0
int main(int argc, char** argv) {
    typedef struct{
        char* nome;
        int idade;
    }PESSOA;
    int numPessoas=3;
    int x;
    PESSOA* pessoas = malloc(numPessoas * sizeof *pessoas);
    printf("Insere 3 pessoas\n\r");
    for (x = 0; x < numPessoas; x++){
        pessoas[x].nome=(char*)malloc(sizeof(char*));
        printf("Insira o nome: ");
        scanf("%s",pessoas[x].nome);
        printf("Insira a idade: ");
        scanf("%d",&pessoas[x].idade);
    }
    for (x = 0; x < numPessoas; x++)
        printf("Nome: %s, idade: %d\n",pessoas[x].nome,pessoas[x].idade);
    return (EXIT_SUCCESS);
}

Compiled and tested with gcc (Linux)

gcc -o main *.c

Run on Linux

./main

Created with editor

nano

Test done on Ubuntu 16.04 LTS

    
05.08.2017 / 21:38