Search code examples
cstringpointersdata-structuresincompatibletypeerror

Incompatible type for argument of strcpy and strcmp


(All strings)I need to check whether a given username(Item) is already logged in by comparing it with a Queue of logged usernames.But when I try to campare them with strcmp i get the error in the title.Later I also have a strcpy to add the username in the Queue with the same error.How can I deal with these problems?

These are my structs

typedef struct{
    char userid[8];
}QueueElementType;

typedef struct QueueNode *QueuePointer;

typedef struct QueueNode
{
    QueueElementType Data;
    QueuePointer Next;
} QueueNode;

typedef struct
{
    QueuePointer Front;
    QueuePointer Rear;
} QueueType;

Code to check given username in the Queue

boolean AlreadyLoggedIn(QueueType Queue, QueueElementType Item){
    QueuePointer CurrPtr;
    CurrPtr = Queue.Front;
    while(CurrPtr!=NULL){
        if(strcmp(CurrPtr->Data,Item.userid) == 0){
            printf("You have logged in to the system from another terminal.New access is forbidden.");
            return TRUE;
        }
        else CurrPtr = CurrPtr->Next;
    }
    return FALSE;
}

Adding the given username to the Queue

void AddQ(QueueType *Queue, QueueElementType Item){
    QueuePointer TempPtr;

    TempPtr= (QueuePointer)malloc(sizeof(struct QueueNode));
    strcpy(TempPtr->Data,Item.userid);
    TempPtr->Next = NULL;
    if (Queue->Front==NULL)
        Queue->Front=TempPtr;
    else
        Queue->Rear->Next = TempPtr;
    Queue->Rear=TempPtr;
}

Solution

  • strcpy(TempPtr->Data,Item.userid);
    strcmp(CurrPtr->Data,Item.userid)
    

    Here Data is of type QueueElementType.

    But strcpy and strcmp takes \0 terminated char * as argument.

    Change it to.

    strcpy(TempPtr->Data.userid,Item.userid);
    strcmp(CurrPtr->Data.userid,Item.userid)