This question is builds on a previous question from me.
There I had this construct. It uses SDL2:
void init_window(SDL_Window *window)
{
window = SDL_CreateWindow(…);
}
int main(void)
{
SDL_Window *window;
init_window(window);
}
This didn't work. The answer suggested, I used *&window
as a function parameter instead, and it worked great.
I rewrote the *&window
to **window
as the following:
void init_window(SDL_Window **window)
{
*window = SDL_CreateWindow(…);
}
int main(void)
{
SDL_Window *window;
init_window(&window);
}
And it also works. But I still don't understand why the first version doesn't work. I looked up the implementation details of SDL_Window and it's just a normal typedef
of a struct to put it into ordinary namespace. SDL_CreateWindow returns SDL_Surface *
.
To picture my dilemma, I wrote this simple program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Person
{
char *name;
int age;
} Person;
Person *person_create(char *name, int age)
{
Person *who = malloc(sizeof(Person));
who->name = strdup(name);
who->age = age;
return who;
}
void person_get_old(Person *who)
{
who->age += 30;
}
int main(void)
{
Person *susan = person_create("Susan", 23);
person_get_old(susan);
printf("%i\n", susan->age);
}
This prints 53
just as expected, without me having to use pointer to pointer semantics. What is the difference between my implementation and that one of SDL2. This is no SDL2 question, as the one that answered my previous question could answer this without any knowledge of SDL2, so there seems to be some implementation detail I missed. Hope somebody can enlighten me.