Here is the structure declare code.
struct list_el {
int val;
struct list_el * next;
};
typedef struct list_el item;
And when I write a function like this, the compiler gives a error. It says cur undeclared before first use
.
bool delete(item* item)
{
assert(item != NULL);
item* cur = NULL;
cur = head;
item* prev = NULL;
while (cur) {
if (cur == item) {
if (prev == NULL) {
head = item->next;
} else {
prev->next = item->next;
}
free(item);
return true;
}
prev = cur;
cur = cur->next;
}
return false;
}
After I look up the reference, it says the typedef
works out just a bit like #define
. It simply makes a substitution at compile time. Is that the reason the code can't be compiled?