1

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;
7
  • 2
    What is the definition of Recept? It looks like you're storing pointers to local variables into uj (which then get copied into receptek and 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. Commented Nov 13, 2021 at 18:43
  • I added the structure onto the bottom of the post Commented Nov 13, 2021 at 18:45
  • You use the pointer (char *nev) to the local variable bekert_nev in the function, which does not live any more. Commented Nov 13, 2021 at 18:50
  • 1
    That looks weird: receptek[(int)hossz] = uj; If hossz really is a pointer, you might get huge values for your array. If it is not a pointer, why do you define it as int*? Commented Nov 14, 2021 at 11:20
  • 1
    "in a different .h file." I hope you don't define that function in a .h file. That would be really bad practice. Instead only put a declaration into the header and put the definition into a .c file. Commented Nov 14, 2021 at 11:21

1 Answer 1

1

In this code memory of local variables bekert_nev and bekert_ot are used outside of the scope where they are defined (function recept_felvesz). They are allocated on stack so after exit from function recept_felvesz their memory can be used by anyone else as it is considered unused. For example printf can store its internal variables in this memory block.

One way to fix this issue is to allocate bekert_nev and bekert_ot in global memory using malloc function:

char* bekert_nev = malloc(30);
char* bekert_ot = malloc(300);

Remember to free this memory with free function when you will destroy your main receptek array:

Recept* rec = receptek;
for( int i = 0; i < hossz; ++i) {
  free( rec->nev );
  free( rec->osszetevok );
  rec++;
}

Alternatively, you can put this arrays inside of your Recept struct and write directly into them

typedef struct Recept {
    int azonosito;
    char nev[30];
    char osszetevok[300];
} Recept;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.