繁体   English   中英

在 C 中创建一维结构数组的问题

[英]issue with creating a one dimensional array of structs in C

所以问题是对于我的项目,我必须为我的操作系统类创建一个逆表页面。 助教写下了一些基本的代码来开始。 要创建表,他说该表应该是一个结构体,其中*包括诸如页大小和沿翻译页数之类的元数据 *表(可以是二维数组,也可以是一维结构数组)所以这里是代码:

#include <stdio.h>
#include <stdlib.h>
#define N 500
struct invTablePage{
    invTablePage page[N];

};

struct table
{
    int numOfPages;
    int pageSize;
    int array[struct invTablePage];
};


void initInverted(struct invTablePage **invTable, int memSize, int frameSize);

int translate(struct invTablePage *invTable, int pid, int page, int offset);

void releaseInverted(struct invTablePage **invTable);

但是当我编译代码时,它给了我这个

error:  expected expression before ‘struct’
error: array type has incomplete element type
  struct invTablePage page[N];

我试过使用 size of 但这不起作用。 显然 int array[struct invTablePage] 可以完成,但我不明白如果尝试获取结构的大小更有意义,那甚至应该如何工作。 至于数组类型有不完整的元素错误,如果我已经声明了我的 invTablePage 类型的结构,我不确定那个错误,所以它应该可以工作。 任何帮助,将不胜感激

struct invTablePage{ invTablePage page[N];

};

你不能像这样定义一个结构。一个结构可以有一个指向它自己类型的指针,但不能有它自己类型的成员。

您要求编译器在定义之前创建一个类型为 invTablePage 的数组。 您需要定义:

struct invTablePage{
    void* page;

};

然后在初始化期间为页面成员分配一个数组:

void function()
{
  invTablePage table_page;
  invTablePage* entry;

   table_page.page = (void*) calloc(N, sizeof(invTablePage));

   /* for example to access element index = 3 */
    entry = (invTablePage*) table_page.page[3];
}

你最好实现一个链表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM