Array of structs in C and ncurses

折月煮酒 提交于 2020-01-06 19:39:36

问题


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;

回答1:


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?



来源:https://stackoverflow.com/questions/22411757/array-of-structs-in-c-and-ncurses

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!