Array of structs in C and ncurses

2019-09-03 08:56发布

I am trying to set up an array of structs that will eventually print off 6 boxes using ncurses. First problem is i dont know how to set up an array of structs, and my second problem is, i dont know how im supposed to draw the boxes. An extra thing about the boxes is that they must be drawn using the "|" key for the vertical walls, and i need to use "-" for the walls going horizontally. I have tried to malloc memory for an array of structs using:

room * roomInfo = malloc(sizeof(room) * 6);

with room being my struct name and roomInfo being my array of structs. I am getting three errors with this. One is "error: unknown type name 'room'" and the other is "error: 'room' undeclared (first use in this function)" (at the top of my file i have: "struct room roomInfo;") and the third being "note: each undeclared identifier is reported only once for each function it appears in"

typedef struct 
{
int roomNumber;
int height;
int width;
int eastDoor;
int westDoor;
int southDoor;
int northDoor;
}room;

标签: c ncurses
1条回答
ゆ 、 Hurt°
2楼-- · 2019-09-03 09:18

Not sure what you are doing wrong: the following minimal code compiles without errors:

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
int roomNumber;
int height;
int width;
int eastDoor;
int westDoor;
int southDoor;
int northDoor;
}room;

int main(void) {
room *roomInfo;
roomInfo = malloc(6*sizeof *roomInfo);
}

Most likely: your definition of room is not known at the time that you declare room *roomInfo;. Is it in a #include?

查看更多
登录 后发表回答