1
You need help with dynamic memory, I can’t understand why I can’t access nome
of ptr
in the method adicionarSocio
?
How can I change this field?
I have to do malloc
of the name before assigning even it already having the size set?
Follow the code below:
typedef struct {
unsigned int nCliente, tel;
char nome[100 + 1];
endereco morada;
emprestimos filmes[30];
} socio;
void adicionarSocio(socio *ptr, int k) {
for (int i = 0; i < k; i++) {
printf("Introduza o seu nome:\n");
char tmp[100 + 1];
scanf(" %s", &tmp);
strcpy(ptr[i].nome, tmp);
}
}
int main() {
int qtde, op;
socio *ap_socio;
printf("Deseja espaço para quantos sócios?\n");
scanf(" %d", &qtde);
ap_socio = (socio*)malloc(qtde * sizeof(socio));
if (ap_socio = NULL)
printf("Erro\n");
else {
do {
do {
printf("Menu:\n");
printf("Adicionar sócio ->1\n");
printf("Alterar sócio ->2\n");
printf("Remover sócio ->3\n");
printf("Listar sócio ->4\n");
printf("Alugueres ->5\n");
printf("Sair - 0\n");
scanf(" %d", &op);
} while (op != 1 && op != 2 && op != 3 && op != 4 && op != 5 && op != 0);
switch (op) {
case 1:
adicionarSocio(ap_socio, qtde);
printf("Sócio adicionado com sucesso!\n");
break;
case 2:
break;
case 3:
break;
case 4:
listarSocio(ap_socio, qtde);
break;
case 5:
break;
}
} while (op != 0);
}
free(ap_socio);
}
What is the purpose of the add-in function? Register a partner ? If so, what is the purpose of the go-to
qtd
?– Isac