I have a problem with my code that I can not solve, I think it's a simple detail, but I have not found it yet.
I need to pass by reference a vector, which has a struct, but it is not allocating the data in the struct and the program closes.
This is one of the functions:
void cadastProd(Produto *cP) {
system("cls");
arquivo = fopen(PRO,"a+");
printf("\n|************************************************|");
printf("\n|* CADASTRAR PRODUTO *|");
printf("\n|************************************************|");
printf("\n\n Informe o numero de produtos: ");
scanf("%d",&q);
for(i=0; i<q; i++) {
(*cP).codigo[i] = i+1; // Codigo do Produto
fflush(stdin);
printf("\n Nome do Produto: ");
scanf(" %[^\n]s", &cP->nome[i]);
printf("\n Valor do Produto: ");
scanf("%f", &cP->valor[i]);
printf("\n ");
printf("%d - %s - R$%.2f\n", cP->codigo[i], cP->nome[i], cP->valor[i]);
fprintf(arquivo,"%d - %s - R$%.2f\n", cP->codigo[i], cP->nome[i], cP->valor[i]);
}
fclose(arquivo);{
}
The struct of this function is this here:
typedef struct produto {
char codigo[30];
char nome[30];
float valor[30];
} Produto;
I'd like some help on what I might be missing. Thanks