I have a structure table which is kind of like a linked list. My goal is to empty whatever is in that table. I think my idea is correct, but it's not working and I do not know why.
typedef struct table{
LIST *top;
int size;
}TABLE;
What I am trying is to take every single thing in the table and apply free() to it. And then to set the top of the table to NULL so I cannot go through it later and also set the size to 0.
void empty_table(TABLE *table) {
LIST *cur;
for (cur = table->top; cur != NULL; cur = cur->next) {
if (cur == NULL) return;
free(cur);
}
table->top = NULL;
table->size = 0;
}
Apparently this is not working and I don't know the reason. I tried a simple run and it doesn't even change the size.
// ...
printf("table has %d elements\n",tab->size);
empty_table(tab);
printf("table has %d elements\n",tab->size);
// ...
Here I am testing it where tab is a pointer to table.
The output is:
table has 5 elements
table has 5 elements