【问题标题】:Array of structs in C and ncursesC和ncurses中的结构数组
【发布时间】:2014-03-14 17:22:24
【问题描述】:

我正在尝试设置一个结构数组,最终将使用 ncurses 打印出 6 个框。第一个问题是我不知道如何设置结构数组,我的第二个问题是我不知道我应该如何绘制框。关于盒子的一个额外的事情是它们必须使用“|”来绘制垂直墙壁的关键,我需要使用“-”来表示水平的墙壁。我尝试使用以下方法为结构数组分配内存:

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

room 是我的结构名称,roomInfo 是我的结构数组。我遇到了三个错误。一个是“错误:未知类型名称'room'”,另一个是“错误:'room' undeclared(第一次在此函数中使用)”(在我的文件顶部我有:“struct room roomInfo;”)和第三个是“注意:每个未声明的标识符对于它出现的每个函数只报告一次”

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

【问题讨论】:

  • 为什么是malloc()?只需使用一个普通的旧结构数组:struct room roomInfo[6];
  • 你有typedef struct {...} room;吗?你能证明它是什么吗?听起来编译器在遇到有问题的行时还没有看到它。
  • 我改变了它,所以我没有使用 malloc,我还将我的结构添加到代码中

标签: c ncurses


【解决方案1】:

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

#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 中吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 2011-07-07
    • 2013-03-25
    • 1970-01-01
    相关资源
    最近更新 更多