结构的C不完整类型

时间:2016-06-13 05:45:34

标签: c

我试图接受RRhead,在RRhead有一个新的RCB点,因为它是下一个,并且在新的RCB中具有RRhead点,因为它是先前的。

typedef struct{
  int sequence_number;
  int file_descriptor;
  FILE *requested_file;
  int bytes_remaining;
  int quantum;
  struct RCB *next;
  struct RCB *prior;
} RCB;

typedef struct RCB RCB;


RCB *RRhead = NULL;


static void admit_to_scheduler_RR(int fd, FILE *fin){
int sequence_counter, new_bytes_remaining, new_quantum = 0;
  RCB new_rcb = {sequence_counter, fd, fin, new_bytes_remaining, new_quantum, RRhead, NULL};
  RRhead->prior = &new_rcb;
  RRhead = &new_rcb;
  sequence_counter++;
}

产生以下错误:

sws.c: In function ‘admit_to_scheduler_RR’:
sws.c:318:10: error: variable ‘new_rcb’ has initializer but incomplete type
   struct RCB new_rcb = {sequence_counter, fd, fin, new_bytes_remaining, new_quantum, RRhead, NULL};
          ^
sws.c:318:10: warning: excess elements in struct initializer [enabled by default]
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default]
sws.c:318:10: warning: excess elements in struct initializer [enabled by default]
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default]
sws.c:318:10: warning: excess elements in struct initializer [enabled by default]
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default]
sws.c:318:10: warning: excess elements in struct initializer [enabled by default]
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default]
sws.c:318:10: warning: excess elements in struct initializer [enabled by default]
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default]
sws.c:318:10: warning: excess elements in struct initializer [enabled by default]
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default]
sws.c:318:10: warning: excess elements in struct initializer [enabled by default]
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default]
sws.c:318:14: error: storage size of ‘new_rcb’ isn’t known
   struct RCB new_rcb = {sequence_counter, fd, fin, new_bytes_remaining, new_quantum, RRhead, NULL};
              ^
sws.c:319:9: error: dereferencing pointer to incomplete type
   RRhead->prior = &new_rcb;
         ^

我不知道为什么我会收到多余元素的警告。我没有正确初始化new_rcb吗?我是否需要创建它然后将所有字段设置为我想要的?

我相信这一切都与new_rcb的“不完整类型”有关。大多数谷歌搜索表明这是因为编译器不知道RCB的大小应该是多少。它似乎表明我需要将此结构定义放入标题中。这绝对必要吗?

4 个答案:

答案 0 :(得分:2)

在第一个声明中,您使用RCB声明structtypedef。紧接着你说,顺便说一下,RCB表示struct RCB',此时此处尚未定义! (为了存在,你必须改为struct RCB { ... };。)

基本上,删除typedef struct RCB RCB;将删除相关错误,但会弹出新错误。

答案 1 :(得分:1)

尝试将结构更改为以下内容(即删除typedef关键字):

struct RCB {
  int sequence_number;
  int file_descriptor;
  FILE *requested_file;
  int bytes_remaining;
  int quantum;
  struct RCB *next;
  struct RCB *prior;
};

Difference between 'struct' and 'typedef struct' in C++?的答案中可以阅读更多信息。

答案 2 :(得分:1)

尝试这样的事情:

typedef struct RCB RCB;
strutc RCB {
 ....

C中的“tag”名称空间(来自structunion)与标识符名称空间不同。使用typedef,您还可以将标识符RCB与类型struct RCB相关联。

答案 3 :(得分:0)

#include <stdio.h>
#include <stdlib.h>

typedef struct RCB RCB;

typedef struct RCB {
    int sequence_number;
    int file_descriptor;
    FILE *requested_file;
    int bytes_remaining;
    int quantum;
    RCB *next;
    RCB *prior;
} RCB;

RCB new_rcb = {11, 21, NULL, 31, 41, NULL, NULL};;

static void admit_to_scheduler_RR(int fd, FILE *fin) {

    int sequence_counter = 0;
    int new_bytes_remaining, new_quantum = 0;
    RCB *RRhead = &new_rcb;
    RRhead->prior = &new_rcb;
    sequence_counter++;
}

int main(int argc, char *argv[]) {

    admit_to_scheduler_RR(1, NULL);
    return 0;
}

要修复错误,您需要将代码更改为类似于上面的代码,它使用您的数据结构和编译。