I'm trying to understand object oriented programming in C. During my research on the internet I came across this code:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int x, y;
int width;
int height;
} Rectangle;
Rectangle *Rectangle_init(int x, int y, int width, int heihgt) {
Rectangle *obj = malloc(sizeof *obj);
obj->x = x;
obj->y = y;
obj->width = width;
obj->height = height;
return obj;
}
void Rectangle_draw(Rectangle *obj) {
printf("I just drew a nice %d by %d rectangle at position (%d, %d)!\n",
obj->width,
obj->height,
obj->x,
obj->y);
}
int main(void) {
Rectangle *r = Rectangle_init(1, 1, 3, 5);
Rectangle_draw(r);
return 0;
}
The code compiles and runs and I do understand pretty much all of it. However what I don't get is why `obj' can be referenced in the malloc-call although it does not exist yet?