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?
Compares the pointer
cur->datato the pointerdata. 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).