繁体   English   中英

来自c中typedef定义的指针的不兼容类型

[英]Incompatible type from typedef defined pointer in c

使用VS 2019,以下C代码功能向我发出C4133警告以及整个代码中的其他几个区域。 警告状态:“警告C4133'=':不兼容的类型-从'client *'到'client_t”

但是,除非我误解了typdef的语法,否则我的typedef client *和client_t应该是同一个人。 以下是收到此警告的一种情况:

//Client information structure for linked list
typedef struct _client {
    char NAME[30];
    unsigned long PHONE;
    unsigned long ID;
    unsigned char CountryID;
    struct client *next;
    struct client *previous;
}client, *client_t;

/*Function to sequentually free every node in the doubly linked list
@param: client_t *head - reference pointer to the head pointer of the client linked list
*/
void RemoveClient(client_t *head) {
    if (head)
    {
        client_t current = *head;

        if (current && current->next) {
            while (current) {
                //Warning C4133 at the below line
                current = (*head)->next;
                free(*head);
                *head = current;
            }
        }
        else
        {
            free(*head);
        }
        current = NULL;
        *head = NULL;
    }
    else printf("head is a NULL pointer");
}

谢谢Cyber​​bission的建议! 将我的结构内部的组件更改为_client,而不是使用稍后给定的client定义来解决很多对我的警告:

//Client information structure for linked list
typedef struct _client {
    char NAME[30];
    unsigned long PHONE;
    unsigned long ID;
    unsigned char CountryID;
    struct _client *next;
    struct _client *previous;
}client, *client_t;

发生的事情是您引用了一个不存在的,名为struct client正向声明类型:

//Client information structure for linked list
typedef struct _client {
    // ...
    struct client *next;
    struct client *previous;
}client, *client_t;

这有点棘手。 在声明nextprevious ,您有一个名为struct _client的类型。 此后不久,您就有了一个名为clienttypedef 不幸的是,这些都不是struct client 由于操作仅引用指针,而没有取消引用指针,因此您没有任何实际错误,但是当您引用next ,编译器会说“呵呵, struct client既不是client也不是struct _client -要警惕!”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM