strcpy和strcmp的参数类型不兼容

时间:2019-04-09 11:11:12

标签: c string pointers data-structures incompatibletypeerror

(所有字符串)我需要检查给定的用户名(Item)是否已与登录的用户名队列进行比较,但是当我尝试使用strcmp对它们进行命名时,标题出现错误。我也有一个strcpy将用户名添加到队列中,但出现相同的错误。如何处理这些问题?

这些是我的结构

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;

用于检查队列中给定用户名的代码

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

将给定的用户名添加到队列中

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

2 个答案:

答案 0 :(得分:1)

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

此处Data的类型为QueueElementType

但是strcpystrcmp\0终止的char *作为参数。

更改为。

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

答案 1 :(得分:0)

尝试

strcmp(CurrPtr->Data.userid,Item.userid)

因为strcmp()期望的参数为const char*,但是CurrPtr->Data的类型为QueueElementType,而不是const char*类型。在strcmp的手册页中。

  

int strcmp(const char * s1,const char * s2);

同样适用于strcpy()。这个

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

设为

strcpy(TempPtr->Data.userid,Item.userid);
相关问题