I’m trying to understand this C code from a simple priority queue, especially – why does it need the struct qnode **first part:
int quedel(struct qnode **first, struct qnode **last, int *prio, int *val) {
struct qnode *tmp = NULL;
if((NULL == *last) && (*last == *first)) {
fprintf(stderr, "Empty queue.....\n");
return -1;
}
*val = (*first)->data, *prio = (*first)->prio;
tmp = *first, *first = (*first)->next;
if(*last == tmp)
*last = (*last)->next;
free(tmp);
return 0;
}
Since C doesn’t have pass-by-reference, only pass-by-value, this approach is the only way to make this assignment:
visible to the caller.
(If
firstwere just a single pointer, and we wrotefirst = first->next, then the code that calls this function wouldn’t see the modification.)