I have a program where I want to add a new element into an array of structs. In my add function it seems to be working, but when I try to use it in the main function, it seems to differ from what's being originally returned.
I have my main.c like this:
int main(void) {
Recept * receptek;
int valasztas;
int *hossz = 0; //A receptek száma.
receptek = betolt("receptek.txt", &hossz);
if (receptek != NULL) {
menu();
printf("Valasztott opcio: ");
while(scanf("%d", &valasztas) == 1) {
if(valasztas == 1) {
Recept * uj = recept_felvesz(receptek, hossz);
printf("%dTH VALUE HERE: %s\n", (int)hossz, uj[(int)hossz].nev);
if(uj != NULL) {
printf("Recept sikeresen felveve.\n\n");
hossz = (int)hossz + 1;
} else {
printf("Nem sikerult a receptet felvenni.\n\n");
}
} else if(valasztas == 2) {
kiir(receptek, (int)hossz);
} else if(valasztas == 6) {
break;
} else {
system("cls");
menu();
printf("Ervenytelen opcio.\n\n");
}
printf("Valasztott opcio: ");
}
} else {
printf("Nem sikerult betolteni a tarolofajlt!\n");
}
return 0;
}
And I store my add function (recept_felvesz) in a different .h file.
Recept* recept_felvesz(Recept* receptek, int* hossz) {
printf("Hossz 1: %d", (int)hossz);
setbuf(stdin, NULL);
char bekert_nev[30];
char bekert_ot[300];
printf("Add meg a recept nevet: ");
if(fgets(bekert_nev, 30, stdin))
bekert_nev[strcspn(bekert_nev, "\n")] = '\0'; //A \n karaktert kicseréli \0-ra
printf("Add meg a recept osszetevoit: ");
if(fgets(bekert_ot, 300, stdin))
bekert_ot[strcspn(bekert_ot, "\n")] = '\0'; //A \n karaktert kicseréli \0-ra.
system("cls");
menu();
Recept uj = {(int)hossz, bekert_nev, bekert_ot};
receptek = (Recept*) realloc(receptek, ((int)hossz + 1) * sizeof(Recept));
receptek[(int)hossz] = uj;
if (receptek == NULL) return NULL;
FILE *file = fopen("receptek.txt", "a");
if (file == NULL) return NULL;
fprintf(file, "\n%s;%s;", bekert_nev, bekert_ot);
fclose(file);
printf("\n%dTH VALUE HERE: %s\n", (int)hossz, receptek[(int)hossz].nev);
return receptek;
}
Here is the buggy output at the moment:
15TH VALUE HERE: 32423
15TH VALUE HERE: ☺
Recept sikeresen felveve.
Why does the actual return value (32423) differ from what's being used in the main function(☺)?
Edit: structure Recept looks like this:
typedef struct Recept {
int azonosito;
char *nev;
char *osszetevok;
} Recept;
Recept? It looks like you're storing pointers to local variables intouj(which then get copied intoreceptekand returned to the caller). Unrelated: Some of your NULL pointer checks are too late, as you'll have dereferenced the NULL pointer before the check.char *nev) to the local variablebekert_nevin the function, which does not live any more.receptek[(int)hossz] = uj;Ifhosszreally is a pointer, you might get huge values for your array. If it is not a pointer, why do you define it asint*?