简体   繁体   中英

Incompatible type from typedef defined pointer in c

Using VS 2019, the following C code function is giving me C4133 warning as well as several other area's throughout my code. The warning states: "Warning C4133 '=': incompatible types - from 'client *' to 'client_t"

However, from my typedef client* and client_t should be the same thing unless I misunderstand the syntax for typdef. Below is one instance where I get this warning:

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

Thank you Cyberbission for your suggestion! Changing my components inside of the struct to _client instead of using the later given definition of client fixed alot of those warnings for me:

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

What's happened is you're referencing a forward declared type that does not exist, named struct client :

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

This is a bit tricky. At the time of your declarations of next and previous , you have a type named struct _client . Shortly thereafter, you have a typedef named client . Neither of those are struct client , unfortunately. Since the operations are only referencing the pointer, but not de-referencing them, you don't have any actual errors, but when you refer to next , the compiler says "huh, struct client is neither client nor struct _client -- be wary!"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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