1

I am attempting to take RRhead, have a new RCB point at RRhead as it's next, and have RRhead point at the new RCB as it's prior.

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

producing the following errors:

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;
         ^

I have no clue about why I am getting the warnings for excess elements. Am I not initializing new_rcb correctly? Do I need to create it and then set all of the fields to what I want?

I believe this all has to do with having an "incomplete type" for new_rcb. Most googling indicates that this is because the compiler doesn't know what the size of RCB should be. It seems to indicate that I need to place this struct definition into a header. Is this absolutely necessary?

Daniel Paczuski Bak
  • 3,720
  • 8
  • 32
  • 78

4 Answers4

2

In the first statement, you are declaring RCB to be a struct using typedef. Directly afterwards, you are saying, 'by the way, RCB means struct RCB', which is not defined at this point! (For that to exist, you'd have to say struct RCB { ... }; instead.)


Basically, delete the typedef struct RCB RCB; which will get rid of the related errors, but new ones will pop up.

thriqon
  • 2,458
  • 17
  • 23
1

Try to change your struct to the following (i.e. remove the typedef keyword):

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

Further information can be read in this answer to Difference between 'struct' and 'typedef struct' in C++?.

Community
  • 1
  • 1
Ely
  • 10,860
  • 4
  • 43
  • 64
1

Try something like this:

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

The "tag" name space in C (coming with struct and union) is different from the identifier name space. With your typedef you also associate the identifier RCB to the the type struct RCB.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
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;
}

To fix your errors, you need to change your code to something similar to the above, which uses your data structures and compiles.

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424