0

I have this types defined:

#define NAME_LENGTH_LIMIT 25
#define LNULL (-1)
#define MAX 1000

typedef char tParticipantName[NAME_LENGTH_LIMIT];
typedef int tNumVotes;
typedef bool tEUParticipant;

typedef struct tItemL {
    tParticipantName participantName;
    tNumVotes numVotes;
    tEUParticipant EUParticipant;
} tItemL;
typedef int tPosL;
typedef struct tList {
    tItemL data[MAX];
    tPosL lastPos;
} tList;

This code below is creating problems, because it needs to read a character but when I insert one I recived this error-> error: invalid operands to binary == (have 'tItemL' and 'tItemL').

tPosL findItem(tItemL d, tList L) {
    tPosL t;
    if (L.lastPos == LNULL) {
        return LNULL;
    } else {
        t = first(L); //p=0;
        while (t != LNULL && (L.data[t] == d)) { //error in this line
            t = next(t, L);
        }
        if (L.data[t] == d) return t;//error in this line 
        else return LNULL;
    }
}

I don't know how to solve the problem

David
  • 3
  • 1

2 Answers2

0
if (L.data[t] == d)

Here L.data[t] and d evaluates to structs.

You can't compare two structs for equality with the == operator.

You need to compare each member separately.

Harith
  • 4,663
  • 1
  • 5
  • 20
0
  1. You should pass the pointer to this big struct, not the struct itself as every time you copy the whole thing when you call the function.
  2. Do hide standard types behind typedefs.
  3. Do not hide arrays behind typedefs
typedef struct tItemL {
    char participantName[NAME_LENGTH_LIMIT];
    int numVotes;
    bool EUParticipant;
} tItemL;

typedef int tPosL;
typedef struct tList {
    tItemL data[MAX];
    tPosL lastPos;
} tList;

int compare(const tItemL *i1, const tItemL *i2)
{
    return strcmp(i1 -> participantName, i2 -> participantName) &&
           i1 -> numVotes == i2 -> numVotes &&
           i1 -> EUParticipant && i1 -> EUParticipant;
}

int findItem(tItemL *d, tList *L) {
    int t = -1;
    if (L -> lastPos == LNULL) {
        return LNULL;
    } else {
        t = first(L); //p=0;
        while (t != LNULL && compare(&L -> data[t], d)) { //error in this line
            t = next(t, L);
        }
        if (compare(&L -> data[t], d)) return t;//error in this line 
        else return LNULL;
    }
}

You need to change other functions to take pointers not struct

0___________
  • 60,014
  • 4
  • 34
  • 74