Home > Mobile >  How to check if the first entry of an array of entries is empty when the array is of type struct in
How to check if the first entry of an array of entries is empty when the array is of type struct in

Time:11-13

I have to write a Program for Uni in which there is supposed to be a queue with a priority and in the queue there is supposed to be an array of entries of which each entry needs to be 31 characters. Also there needs to be a function that checks if the queue is empty or full. I have written this code but it doesnt work, I get the error invalid oprands to binary == (have entry and 'int'):

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

enum priority {
    H, h, n, l, L      //Priority enum Highest = 0 Lowest = 4
};

typedef struct entry{
    enum priority priorityvar;
    char message[31];
} entry;

typedef struct pqueue {
    entry entries[10];
} pqueue;

int isEmpty(pqueue* pqueue){
    if (pqueue->entries[0] == 0)
        return 1;
    else
        return 0;
}
int isFull(pqueue* pqueue){
    if (pqueue->entries[9] != 0)
        return 1;
    else
        return 0;
}

I also tried to replace the 0 with a NULL but that didnt work either, I would be so grateful for any help! Thank you so much!!

Ps: I also have to write a function later in which the first entry is being printed and deleted while all the other etries move 1 "up" in the queue and I have no Idea how to implement that. If somebody has ideas thank you so much!!

CodePudding user response:

pqueue is a pointer in the function isEmpty, it points to an array of entries. You are accessing the array entries, when you write pqueue->entries[0] you are accessing the element of type entry, but this isn't a pointer to check if it's NULL! What you should do is create a variable, say next in the struct pqueue that tells you where the next empty spot is, give it an initial value of -1, and you will check if its -1, then your queue is empty, and so on, when adding an element increment it by 1, before adding.

  • Related