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

时间:2019-04-28 01:49:10

标签: c warnings

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

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

//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");
}

2 个答案:

答案 0 :(得分:0)

感谢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;

答案 1 :(得分:0)

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

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

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