C和ncurses中的结构数组 [英] Array of structs in C and ncurses

查看:252
本文介绍了C和ncurses中的结构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个结构体数组,这些结构体最终将使用ncurses打印出6个框.第一个问题是我不知道如何设置结构数组,而第二个问题是,我不知道即时消息该如何绘制盒子.关于盒子的另一件事是,必须使用"|"绘制它们垂直墙壁的关键,而我需要水平墙壁使用-".我试图使用以下方法为结构数组分配内存:

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);

其中room是我的结构名称,而roomInfo是我的结构数组.我与此有关的三个错误.一个是错误:未知类型名称'room'",另一个是错误:未声明'room'(此功能的首次使用)"(在我文件的顶部,我具有:"struct room roomInfo;")和第三个是注意:每个未声明的标识符针对出现在其中的每个功能仅报告一次"

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;

推荐答案

不确定您在做什么:以下最小代码编译没有错误:

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);
}

最可能的是:在声明room *roomInfo;时,您对room的定义是未知的.是在#include中吗?

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

这篇关于C和ncurses中的结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆