【问题标题】:Incompatible type for argument of strcpy and strcmpstrcpy 和 strcmp 的参数类型不兼容
【发布时间】:2019-04-09 11:11:11
【问题描述】:

(所有字符串)我需要通过将给定的用户名(项目)与记录的用户名队列进行比较来检查它是否已经登录。但是当我尝试使用 strcmp 对它们进行露营时,我得到标题中的错误。稍后我也有一个strcpy在Queue中添加用户名,同样报错。这些问题该如何处理?

这些是我的结构

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

【问题讨论】:

  • if(strcmp(CurrPtr->Data,.. 数据不是 strcmp() 所需的 const char*

标签: c string pointers data-structures incompatibletypeerror


【解决方案1】:
strcpy(TempPtr->Data,Item.userid);
strcmp(CurrPtr->Data,Item.userid)

这里DataQueueElementType 类型。

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

改成。

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

【讨论】:

  • 感谢您的快速回答。确实我需要连接到用户 ID。
【解决方案2】:

试试

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

因为strcmp() 需要const char* 的参数,但CurrPtr->DataQueueElementType 类型而不是const char* 类型。来自strcmp的手册页。

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

同样适用于strcpy()。这个

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

制作成

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-28
    • 2017-09-02
    • 2018-02-27
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多