I am trying to create objects in c using turbo c. I am having trouble defining attributes in them.
/*
code for turbo c
included conio.h and stdio.h
*/
typedef struct {
int topX;
int topY;
int width;
int height;
int backgroundColor;
}Window;
typedef struct {
Window *awindow;
char *title;
}TitleBar;
Window* newWindow(int, int, int, int, int);
TitleBar* newTitleBar(char*);
void main() {
TitleBar *tbar;
tbar = newTitleBar("a title");
/*
the statement below echos,
topX:844
topY:170
instead of
topX:1
topY:1
*/
printf("topX:%d\ntopY:%d", tbar->awindow->topX, tbar->awindow->topY);
/*
where as statement below echos right value
echos "a title"
*/
printf("\ntitle:%s", tbar->title);
//displayTitleBar(tbar);
}
Window* newWindow(int topX, int topY, int width, int height, int backgroundColor) {
Window *win;
win->topX = topX;
win->topY = topY;
win->width = width;
win->height = height;
win->backgroundColor = backgroundColor;
return win;
}
TitleBar* newTitleBar(char *title) {
TitleBar *atitleBar;
atitleBar->awindow = newWindow(1,1,80,1,WHITE);
atitleBar->title = title;
return atitleBar;
}
What am i doing wrong?
What is the proper way of defining structures?
You just declare a pointer:
And try to write there in the next lines, while the pointer still doesn’t point to any valid object:
You probably wanted to create a new object:
The same with TitleBar.