Doubt about struct and list in c

Asked

Viewed 396 times

1

I’m having trouble when you need to give a value to a person, in case I need to give the value name and age to the person. I tried to create a struct PESSOA and a list containing a person and an id, but during the build shows me the image error.

erro

Follow my code below:

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");
        gets(no_proximo->pess().nome);
        printf("Insira a idade");
        scanf("%d", &no_proximo->pess->idade);
        no_proximo->id = id;
}
}
  • I fixed the formatting for you.

2 answers

2

So you must 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 don’t know if this program will do what you want however.

Some remarks:

  • Never, ever, in any way, dare to think about using the function gets. This function is unanimously hated and detested in the C-programmer community for being badly designed and extremely insecure. In your code, I replaced it with a secure but equivalent call fgets.

  • Don’t confuse No with NO. The name No is what you gave as a nickname for struct NO. The name NO isolated nothing means. No use putting No within the declaration of { ... } of own No because it creates an egg and chicken problem: So that the compiler knows what it is No within the structure, it already has to have finished reading the structure. However, when using struct NO, compiler has seen the header before, although it has not finished reading the structure, and so there is no problem.

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

  • Just 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 insert parentheses after the name of a field of any struct (be accessed by -> or by .).

  • Thank you, you solved everything.

  • 1

    @Winefrance In this case, if you have no doubt, click on what appears below the voting number of this answer to mark it as accepted and mark your question as resolved. It was a pleasure to help you.

0

Simplified Version - Tested

Create a file called 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

Browser other questions tagged

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