简体   繁体   中英

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. 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. 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. Any help would be appreciated

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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