0

I'm doing an exercise for my programming subject, it's a Hospital Manegment program. And in the beginning of the program i need to send the information from a .txt to a dynamic struct. And I've been battling the last few hours why am I losing the data.

Why does my head pointer is null after exiting this function and i try to print it out ? Shouldn't the pointer keep the information.

Thanks in advance. Ps: If you have any tip for my algorithm feel free to say.

void dump_med(struct medico *head_m)
{
    int i = 1;
    struct medico *new_med;
    struct medico *temp_m;

    FILE *f = fopen(M_FILE, "r");
    new_med = malloc(sizeof(struct medico));
    temp_m = malloc(sizeof(struct medico));

    if(f == NULL)
    {
        printf("Erro a abrir o ficheiro\n");
        return;
    }

    if( new_med == NULL || temp_m == NULL)
    {
        printf("Erro a alocar a memória\n");
        return;
    }
    while(!feof(f))
    {   
        new_med = realloc(new_med, sizeof(struct medico) * i);
        if( new_med == NULL)
        {
            printf("Erro a realocar a memória\n");
            return;
        }
        fscanf(f, "%[^\n]", new_med->nome);
        fscanf(f, "%s %d.%d--%d.%d\n", new_med->especialidade, &(new_med->entrada.h), 
            &(new_med->entrada.m), &(new_med->saida.h), &(new_med->saida.m));
        temp_m = new_med;
        temp_m->next = head_m;
        head_m = temp_m;
        new_med = new_med->next;
        i++;

    }
    free(new_med);
    free(temp_m);
    fclose(f);
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
m3k3r1
  • 15
  • 6
  • I question the mechanics of how you're trying to build a forward-chained linked list here. There are much, *much* simpler ways to do this. – WhozCraig Jul 29 '16 at 18:50
  • I'd be thankful if you guide me to a simpler way that was my 5th attempt of the linked list the other times i was always losing the head position. – m3k3r1 Jul 29 '16 at 19:29
  • [Something like this](http://pastebin.com/XESzqnap), invoked using a provided filename and the adress of a node pointer that will receive the list being generated. Good luck. Any future "how can this be better" questions on this should probably be foisted on [codereview.stackexchange.com](http://codereview.stackexchange.com). – WhozCraig Jul 29 '16 at 19:33
  • Wow it's really a lot more simple and clearer! I'll try to adapt this to my code and check codereview. Thank you!!! – m3k3r1 Jul 29 '16 at 19:53

2 Answers2

1

In C, all parameters are pass by value. So when modify head_m, you're only modifying a local variable.

If you want changes to be reflected in the calling function, pass the address of the variable in question, then dereference the pointer variable in the function.

void dump_med(struct medico **head_m)
{
    ...
    temp_m->next = *head_m;
    *head_m = temp_m;
    ...
}

Then you call it like this:

dump_med(&head_m);
dbush
  • 205,898
  • 23
  • 218
  • 273
1

C uses pass-by value for function parameter passing. If you're going to modify the head_m inside the function, it will not be reflected to the actual argument. You need to either

  • Pass a pointer to the pointer to be modified.
  • Have a return statement, returning the new value.

That said,

  1. Please see Why is “while ( !feof (file) )” always wrong?
  2. Always check the return value of scanf() and family.
Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Thanks for the tips I'll try to stop using feof and I'm used to check the return values of scanf() more deeply when i'm almost done with the base skeleton of the program – m3k3r1 Jul 29 '16 at 19:27