CodeKingPlusPlus
CodeKingPlusPlus

Reputation: 16081

C void pointers and pointer comparison

I have the following function to remove generic data from a linked list in C:

void removeData(void *data, struct accList *theList)
{
    struct accListNode* cur = theList->head;
    struct accListNode* prev = NULL;

    for(; cur != NULL; prev = cur, cur = cur->next)
    {
        if(cur->data == data)
        {
            if(cur == theList->head)
            {
               theList->head = cur->next;
            }
            if(cur == theList->tail)
            {
                theList->tail = prev;
            }
            if(prev != NULL)
            {
                prev->next = cur->next;
            }
            free(cur);
            return;
        }
    }
}

What is the meaning behind cur->data == data?

Since my data is generic (void*), what does this mean for any primitive type and any structure type?

For example, consider the employee structure:

struct employee
{
    char name[20];
    float wageRate;
};

How would the statement cur->data == data work if data is of type struct employee*? Since data is a pointer to the first memory address of the structure am I just comparing pointer addresses?

Upvotes: 3

Views: 6349

Answers (4)

Ed Swangren
Ed Swangren

Reputation: 124692

cur->data == data

Compares the pointer cur->data to the pointer data. You're comparing their values, not their addresses. A pointer is a variable like any other. It has an address (i.e., &some_ptr) and a value (i.e., the address of the thing it refers to).

Note that other types of comparisons (i.e., < > >= <=) result in undefined behavior unless the pointers point to elements of the same array or one past the end (not that it would make sense to do so unless you knew they pointed to "objects" that resided in the same chunk of contiguous memory anyway, but still).

Upvotes: 7

phonetagger
phonetagger

Reputation: 7873

I’m guessing that you didn’t write removeData()? If not, then my explanation is:

removeData() is designed to remove a specific entry (i.e. *data) from the linked list, not an entry whose content equals *data. So a caller of removeData() has to already know exactly where the entry is that they want free()’ed. They could free() it themselves, but that wouldn’t be nice because the linked list still contains a pointer to it, and presumably other users of the linked list would think the free()’ed object was still valid (they would have no reason to believe otherwise, absent any other scheme of telling them such).

If you want a function that searches theList for an entry whose content matches the content of *data (but is not at the same memory as *data), then you’ll need to write a version of removeData() for each such type, and your accList will either need to be homogeneous (containing all objects of that type), or else accList will need to also contain some indication of each entry’s type, perhaps as an enum, and each version of removeData() (one for each type) will need to skip elements that don’t match its own type.

Upvotes: 0

dinox0r
dinox0r

Reputation: 16039

What is the meaning behind cur->data == data?

The code is checking is both pointers are the same pointer

Since my data is generic (void*) what does this mean for any primitive type and any structure type?

How would the statement cur->data == data work if data is of type struct employee*?

Nothing, the comparison works the same

Since data is a pointer to the first memory address of the structure am I just comparing pointer addresses?

Nope, you're comparing pointers

Upvotes: 3

tommyo
tommyo

Reputation: 551

You are comparing the pointers thermself, not the struct pointed to.

Upvotes: 2

Related Questions