créer un nouvel objet à l'aide de la structure en c
Je suis en train de créer des objets en c à l'aide turbo c
. J'ai de la difficulté de la définition d'attributs en eux.
/*
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;
}
Ce que je fais mal?
Quelle est la bonne manière de définir les structures?
Si elle n'est pas une faute de frappe alors une partie importante de votre code est arriver a commenté le commentaire n'est pas fermé sur la ligne numéro 4.
OriginalL'auteur Asur | 2012-03-05
Vous devez vous connecter pour publier un commentaire.
Que vous venez de déclarer un pointeur:
Et d'essayer d'écrire dans les prochaines lignes, alors que le pointeur ne pointe pas vers quelque objet valide:
Que vous avez probablement envie de créer un nouvel objet:
De même avec la barre de titre.
OriginalL'auteur peper0
Votre pointeurs ne sont jamais réellement affecté à quoi que ce soit. En C, un pointeur n'existe pas par lui-même, il a besoin d'un objet réel dans la mémoire de point de.
Cette page a plus.
OriginalL'auteur ComicSansMS