I rewrote the code to show more or less how it is done. Note that there is still a lot of improvement in it. There are errors that can happen easily and are being checked, other details, but that's how you really do it, even something simple.
Placing multiple arrays within
struct
does not make any sense, what you want is simpler and more viable than this. Your calendar%% must only have the size and a pointer to the contact list. This is already conceptually correct and makes it easy to do the rest.
I created a new structure with the contact entry. If you want you can call it contact, but I do not know if it's suitable in a real context. In it will have the number and the name. The number I changed to string because it's usually the phone number, so struct
is not appropriate . If it's something else, then it seems to make less sense to even let the person type this.
I have predefined that the name will have 30 characters, you can use the size you want.
I better control typing. You have flaws in using int
so, but for an exercise that's fine.
I've given a better name to the function that initializes the calendar so that it's clear that you need to release it. And I created it before any way, even to avoid another mistake. And I changed the startup to reboot. There is an error there in the reboot, which I have in some way introduced, see if you can identify and fix it, so exercise really.
If you do not understand some point ask specific questions.
I made other simplifications and improvements. Since you want to learn pay attention to every detail, even in the spaces. I had others wrong that I corrected, it gets exercise to identify them.
#include <stdio.h>
#include <malloc.h>
#define MAX 50
typedef struct {
char numero[12]; //permite um telefone nacional
char nome[31]; //permite 30 caracteres
} Entrada;
typedef struct {
int tamanho;
Entrada *contatos;
} Agenda;
void cadastrar(Agenda *agenda) {
int op = 1;
do {
printf("Digite o telefone\n");
scanf("%11s", agenda->contatos[agenda->tamanho].numero);
printf("Digite o seu nome\n");
scanf(" %30[^\n]", agenda->contatos[agenda->tamanho].nome);
printf("Digite 1 para continuar 2 para parar\n");
scanf("%d", &op);
agenda->tamanho++;
} while (op != 2);
}
void imprime(Agenda agenda) {
for (int i = 0; i < agenda.tamanho; i++) printf("%11s - %s\n", agenda.contatos[i].numero, agenda.contatos[i].nome);
}
Agenda iniciaAlocaAgenda() {
return (Agenda) { .tamanho = 0, .contatos = malloc(sizeof(Entrada) * MAX) };
}
void liberaAgenda(Agenda agenda) {
free(agenda.contatos);
}
int main() {
Agenda agenda = iniciaAlocaAgenda();
int op = 1;
do {
printf("1-Adicionar numero\n");
printf("2-Remover numero\n");
printf("3-Procurar numero\n");
printf("4-Imprimir\n");
printf("6-Reincia a agenda\n");
printf("7-Parar\n");
scanf("%d", &op);
switch (op) {
case 1:
cadastrar(&agenda);
break;
case 2:
case 3:
break;
case 4:
imprime(agenda);
break;
case 5:
break;
case 6:
agenda = iniciaAlocaAgenda();
break;
}
} while (op != 7);
liberaAgenda(agenda);
}
See running on ideone . And no Coding Ground . Also I put it in GitHub for future reference .