Eliaz
Eliaz

Reputation: 75

printf not working outside a function with a pointer to struct

Everything is heavily simplified from my project which is why there is only one int in the struct here for example.

In the .h :

typedef struct
{
    int id;
}Jeux;

And in the .c:

void filesLoad(Jeux* tJeux){
    (...)
    tJeux = malloc(sizeof(Jeux));
    fscanf(flot,"%d%*[^\n]\n", &tJeux->id);
    printf("id stocked: %d\n", tJeux->id);
    fclose(flot);
}

int main(void){
Jeux* tJeux;
filesLoad(tJeux);
printf("id stocké: %d\n", tJeux->id);
}

Now the problem: For some reason, the printf in the function works perfectly fine but the one outside filesLoad crashes my program, help ?

Upvotes: 0

Views: 149

Answers (4)

IrAM
IrAM

Reputation: 1738

If you are allowed to change the prototype of filesLoad then you can use any of the below methods.

If you are not allowed to change, then the way suggested by @AKX is the way to go.

Method 1. return the address of allocated memory ( call by value )

Jeux* filesLoad(void) {
    (...)
    Jeux* tJeux = malloc(sizeof(Jeux));
    fscanf(flot,"%d%*[^\n]\n", &tJeux->id);
    printf("id stocked: %d\n", tJeux->id);
    fclose(flot);
    return tJeux;
}

int main(void){
    Jeux* tJeux;
    tJeux = filesLoad(tJeux);
    printf("id stocké: %d\n", tJeux->id);
}

Method 2: pass the address ( call by reference )

void filesLoad(Jeux** tJeux){
    (...)
    *tJeux = malloc(sizeof(Jeux));
    fscanf(flot,"%d%*[^\n]\n", &((*tJeux)->id));
    printf("id stocked: %d\n", *tJeux->id);
    fclose(flot);
}

int main(void){
    Jeux* tJeux;
    filesLoad(&tJeux);
    printf("id stocké: %d\n", tJeux->id);
}

Upvotes: 1

jpkmiller
jpkmiller

Reputation: 45

I think it should be sth. like:

void filesLoad(Jeux* tJeux)
{
    (...)
    fscanf(flot,"%d%*[^\n]\n", &tJeux->id);
    printf("id stocked: %d\n", tJeux->id);
    fclose(flot);
}

int main(void)
{
   Jeux* tJeux = (Jeux*) malloc(sizeof(Jeux));
   filesLoad(tJeux);
   printf("id stocké: %d\n", tJeux->id);
}

It should be done in the main, because tJeux was allocated in filesLoad the stack of the function, and it would be removed after returning from the function.

EDIT: sorry, I don't have much experience with giving answers in stack overflow. Thank you for the critics.

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881793

If you want to change something in a function that exists outside of that function, you emulate pass-by-reference by passing in a pointer to it and dereferencing that pointer:

void change(int *pVal) { *pVal = 42; }
int val = 7;
change(&val);
// Now it's 42.

That's no different to changing a pointer but you have to pass in a pointer to the pointer, something like:

void filesLoad(Jeux **ptJeux){
    *ptJeux = malloc(sizeof(Jeux));
    fscanf(flot,"%d%*[^\n]\n", &(*ptJeux)->id);
    printf("id stocked: %d\n", (*ptJeux)->id);
    fclose(flot);
}

int main(void){
    Jeux* tJeux;
    filesLoad(&tJeux);
    :
}

Upvotes: 1

AKX
AKX

Reputation: 169154

You're reassigning tJeux within the function to a freshly allocated piece of memory, but that change is never propagated back up to main.

Instead maybe make filesLoad write into a pre-allocated object:

typedef struct { int id; } Jeux;

void filesLoad(Jeux *tJeux) {
  // ...
  fscanf(flot, "%d%*[^\n]\n", &tJeux->id);
}

int main(void) {
  Jeux *tJeux = malloc(sizeof(Jeux));
  filesLoad(tJeux); // Load a file into `tJeux`
  printf("id stocké: %d\n", tJeux->id);
}

Additionally, you could have a function that allocates and loads a Jeux (but do remember it will then be the caller's responsibility to eventually free the Jeux):

typedef struct { int id; } Jeux;

void filesLoad(Jeux *tJeux) {
  // ...
  fscanf(flot, "%d%*[^\n]\n", &tJeux->id);
}

Jeux* allocateAndFilesLoad() {
  Jeux* tJeux = malloc(sizeof(Jeux));
  filesLoad(tJeux);
  return tJeux;
}

int main(void) {
  Jeux *tJeux = allocateAndFilesLoad();
  printf("id stocké: %d\n", tJeux->id);
}

Upvotes: 3

Related Questions