Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assignment from incompatible pointer type (structs, linked list)

Tags:

c

pointers

Creating a dictionary data structure using a linked list.

typedef struct _dictionary_entry_t
{
    const char* key;
    const char* value;
    struct dictionary_entry_t *next;
    struct dictionary_entry_t *prev;

} dictionary_entry_t;

typedef struct _dictionary_t
{   
    dictionary_entry_t *head;
    dictionary_entry_t *curr;
    int size; 

} dictionary_t;

Working on the function to add dictionary entries to the linked list.

int dictionary_add(dictionary_t *d, const char *key, const char *value)
{
    if (d->curr == NULL) //then list is empty
    {
        d->head = malloc(sizeof(dictionary_entry_t));
        d->head->key = key;  //set first dictionary entry key
        d->head->value = value; //set first dictionary entry value
        d->head->next = NULL; 
        //d->curr = d->head;
    }

    else 
    {
        d->curr = d->head;

        while (strcmp((d->curr->key), key) != 0 && d->curr != NULL) //while keys don't match and haven't reached end of list... 
        {
            d->curr = d->curr->next; 

        } 
    }


    return -1;
}

assigning d->curr to d->curr->next gives me the warning 'assignment from incompatible pointer type'.

What is my mistake here? both curr and next are of the type *dictionary_entry_t

like image 884
Collin Avatar asked Aug 31 '25 01:08

Collin


1 Answers

next is a struct dictionary_entry_t *, but d->curr is a dictionary_entry_t * aka struct _dictionary_entry_t *. Note the difference in underscores.

One way to solve this would be to be consistent with your underscores, declaring next as:

struct _dictionary_entry_t *next;

However, I prefer a different way: typedeffing before declaring the struct. Then:

typedef struct _dictionary_entry_t dictionary_entry_t;
struct _dictionary_entry_t {
    /* ... */
    dictionary_entry_t *next;
    /* ... */
};
like image 90
icktoofay Avatar answered Sep 03 '25 23:09

icktoofay