-2
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct{
    char nome[40];
    char telefone[15];
    char celular[15];
    char email[40];
    struct pessoa *prox;
} pessoa;

pessoa * criarLista(){
  return NULL;
}

pessoa * inserir(pessoa *lista,char nome[40],char telefone[15],char celular[15],char email[40]){

  pessoa *novo = (pessoa *) malloc(sizeof(pessoa));
  strcpy(novo -> nome, nome);
  strcpy(novo -> telefone, telefone);
  strcpy(novo -> celular, celular);
  strcpy(novo -> email, email);
  novo -> prox = lista;
  return novo;
  
}
pessoa * busca(pessoa*lista,char nome[40]){
  pessoa *p = lista;
    while(p!=NULL){
    if (strcmp(nome,p->nome) == 0){
      return p;
    }
    else{
      p = p->prox;
    } 
  }
  return NULL;
}


void exibir(pessoa * lista){
  pessoa *p = lista;
  while(p!=NULL){
    printf("\n%s",p->nome);
    printf("%s",p->email);
    printf("%s",p->telefone);
    printf("%s",p->celular);
    p = p->prox;
  }
}

pessoa* remover(pessoa **lista, char nome[40]){
    pessoa *aux, *remover = NULL;

    if(*lista){
        if(strcmp((*lista)->nome,nome) == 0){
            remover = *lista;
            *lista = remover->prox;
        }
        else{
            aux = *lista;
            while(aux->prox && strcmp(aux->prox->nome,nome)!= 0){
                aux = aux->prox;
            if(aux->prox){
                remover = aux->prox;
                aux->prox = remover->prox;
            }
        }
          }
    }
    return remover;
}

int main(void){
  char nome[40];
  char telefone[15];
  char celular[15];
  char email[40];
  pessoa *lista = (pessoa *) malloc(sizeof(pessoa));
  pessoa *b = (pessoa *) malloc(sizeof(pessoa));
  lista = criarLista();
  b=criarLista();
  int  op;
  while (op!=5){
    printf("\n1- inserir contato \n2- listar contatos \n3- buscar contatos \n4- deletar contato\n5- sair\n");
    scanf("%d", &op);
    setbuf(stdin, NULL);
  
    switch (op) {
      case 1:
        printf("Digite o nome: ");
        fgets(nome,sizeof(nome),stdin);
        printf("Digite o email: ");
        fgets(email,sizeof(email),stdin);
        printf("Digite o telefone: ");
        fgets(telefone,sizeof(telefone),stdin);
        printf("Digite o celular: ");
        fgets(celular,sizeof(celular),stdin);
        lista=inserir(lista,nome,telefone,celular,email);
        break; 
      case 2:
        exibir(lista);
        break;
      case 3:
        printf("Digite o nome que deseja buscar: ");
        fgets(nome,sizeof(nome),stdin);
        b = busca(lista,nome);
        if (b != NULL){
          printf("%s",b->nome);
          printf("%s",b->email);
          printf("%s",b->telefone);
          printf("%s\n",b->celular);
        }
        else{
          printf("Contato não encontrado!");
        }
        break;
      case 4:
        printf("Digite o nome do contato que deseja excluir: ");
        fgets(nome,sizeof(nome),stdin);
        lista = remover(&lista,nome);
    }
  }
  return 0;
}

Tried to put an array that creates a new knot, list all of them, search and remove. But when i was trying to make the remove function, something went wrong and i cound't proceed.The error was aparrently in the function remover, a "incomplete definition of type 'struct pessoa'" error, what is strange cause my typedef struct (prox) works in all of the others functions. Can someone please help me?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    Style guide: the dot `.` and arrow `->` operators bind very tightly because they are [postfix operators](http://port70.net/~nsz/c/c11/n1570.html#6.5.2.3). They should not be written with spaces around them. Writing `nove -> nome` is not idiomatic C and indicates that the coder is a tyro (newbie). Use `nove->nome`. – Jonathan Leffler Nov 30 '22 at 22:34

1 Answers1

1

You have:

typedef struct {
    char nome[40];
    char telefone[15];
    char celular[15];
    char email[40];
    struct pessoa *prox;
} pessoa;

You've defined an untagged structure type called pessoa; you have not defined a type struct pessoa. This is legitimate, but not what you wanted.

The fix is simple:

typedef struct pessoa
{
    char nome[40];
    char telefone[15];
    char celular[15];
    char email[40];
    struct pessoa *prox;
} pessoa;

Now you have both a struct pessoa and (after this typedef is complete), a type name pessoa.

Background

As noted, the notation used in the question's code is legitimate, but not what you wanted. For self-referential structures like you want, you have to use a tag. You could use:

typedef struct pessoa pessoa;
struct pessoa
{
    char nome[40];
    char telefone[15];
    char celular[15];
    char email[40];
    pessoa *prox;        // struct not needed, though still valid
};

Being able to reference struct tagname without defining what's in the structure allows you to have 'opaque types' where client code can only use pointers to the type, and only the implementation functions can see the internals of the type. This is often beneficial as a way of hiding information.

Note that the language makes no requirement that the tag name and the typedef name are related in any way. However, it is conventional and sensible to make sure that the tag names and the typedef names are, in fact, related, so it is easier for people reading the code. Note that tag names are in a separate namespace from typedef names.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278