简体   繁体   English

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

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

So the issue is for my project I have to pretty much create an inverse table page for my operating systems class.所以问题是对于我的项目,我必须为我的操作系统类创建一个逆表页面。 The TA wrote down some basic code to get started.助教写下了一些基本的代码来开始。 To create the table, he said the table should be a struct that * includes metadata such as page size and the number of pages along the translation * table (that can be a 2-dimensional array, or a one-dimensional array of structs) so here is the code:要创建表,他说该表应该是一个结构体,其中*包括诸如页大小和沿翻译页数之类的元数据 *表(可以是二维数组,也可以是一维结构数组)所以这里是代码:

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

however when i compile the code it gives me this但是当我编译代码时,它给了我这个

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

I have tried using size of but that doesnt work.我试过使用 size of 但这不起作用。 Apparently int array[struct invTablePage] can be done but i dont understand how that even is supposed to work if it makes more sense trying to get the size of the struct.显然 int array[struct invTablePage] 可以完成,但我不明白如果尝试获取结构的大小更有意义,那甚至应该如何工作。 As far as array type has incomplete element error, i am not sure about that one if i already declared my struct of type invTablePage so it should be working.至于数组类型有不完整的元素错误,如果我已经声明了我的 invTablePage 类型的结构,我不确定那个错误,所以它应该可以工作。 Any help would be appreciated任何帮助,将不胜感激

struct invTablePage{ invTablePage page[N]; struct invTablePage{ invTablePage page[N];

}; };

You cannot define a structure like this.A structure can have a pointer to it's own type but cannot have a member of it's own type.你不能像这样定义一个结构。一个结构可以有一个指向它自己类型的指针,但不能有它自己类型的成员。

You're asking the compiler to create an array of type invTablePage before it has been defined.您要求编译器在定义之前创建一个类型为 invTablePage 的数组。 You need to define:您需要定义:

struct invTablePage{
    void* page;

};

Then allocate an array for the page member during initialisation:然后在初始化期间为页面成员分配一个数组:

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

You'd be better implement a linked list.你最好实现一个链表。

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

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