I'm working on a function in C that should return a pointer to an array struct. The struct is
struct corr{
char nome[128];
char cognome[128];
char password[10];
float importo;
};
typedef struct corr correntista;
The function that return a pointer to this struct is
correntista *max_prelievo(FILE *fcorrentisti, FILE *fprelievi){
correntista *corr_max[2];
corr_max[0] = (correntista *)malloc(sizeof(correntista));
corr_max[1] = (correntista *)malloc(sizeof(correntista));
/*.........*/
return *corr_max;
}
In the main program I want to print the returned value in the following way:
*c_max = max_prelievo(fc, fp);
printf("Correntista con max prelievi:\n");
printf("Nome: %s\n", c_max[0]->nome);
printf("Cognome: %s\n", c_max[0]->cognome);
printf("Max prelievo: %f\n\n", c_max[0]->importo);
printf("Correntista con max versamenti:\n");
printf("Nome: %s\n", c_max[1]->nome);
printf("Cognome: %s\n", c_max[1]->cognome);
printf("Max versamento: %f\n", c_max[1]->importo);
but only the first struct c_max[0]
has the expected value. c_max[1]
has garbage values. What should I change in my program?