Well, it shouldn't as far as I can tell. I'll provide context first. I'm wanting to define "Controls" as basically screen widgets:
typedef struct {
struct Control** children;
SDL_Surface* surface;
struct Control* parent;
char* type;
int width;
int height;
int x;
int y;
} Control;
The main screen is called a Window, and I made a special init function for it:
Control* createWindow() {
Control window = {
.type = "window",
.surface = SDL_SetVideoMode(WIN_W, WIN_H, 24, SDL_SWSURFACE),
.height = WIN_H,
.width = WIN_W,
.x = 0,
.y = 0
};
return &window;
}
It has a child that's called a Panel, with its own initializer:
Control* createPanel(const char* filepath, Control* parent, int x, int y, int numberOfChildren) {
Control panel = {
.parent = parent,
.children = (Control**)malloc(numberOfChildren * sizeof(Control*)),
.type = "panel",
.x = x,
.y = y,
.surface = SDL_LoadBMP(filepath)
};
return &panel;
}
And the beginning of the main function:
int main(int argc, char* args[]) {
Control* window = NULL;
Control* panel = NULL;
SDL_Init(SDL_INIT_EVERYTHING);
window = createWindow();
panel = createPanel("BACKGROUND.bmp", window, 0, 0, 3);
Now, when reaching the createWindow()
function, everything is fine and window
is well defined. After the next line, the panel initialization, window
gets mangled. I just can't figure out why.
I thought it might have been because I sent window
to be assigned as panel
's parent, so I tried without passing it and removing that assignment. No go, after createPanel()
returns, it still messes up the fields of window
in the main scope.
I've debugged this issue for a long time now and I just have no leads left. I'm pretty new to C and pointer anomalies can happen and I wouldn't know, so I'm really expecting this to be something totally trivial..
Thank you for your time.