I am trying to remove an element from a linked list using structures in C. The part that makes no sense is that I wrote a function for it and the same body of the function when placed in the main function produces different results.
#include<stdio.h>
struct entry {
int value;
struct entry *next;
};
void removeEntry(struct entry *prior) {
prior = prior -> next;
}
int main(void) {
struct entry e1;
e1.value = 1;
struct entry listPointer;
listPointer.next = &e1;
struct entry e2;
e2.value = 2;
struct entry e3;
e3.value = 3;
e1.next = &e2;
e2.next = &e3;
e3.next = (struct entry *) 0;
removeEntry(listPointer.next);
printf("%i\n", listPointer.next -> value);
listPointer.next = listPointer.next -> next;
printf("%i\n", listPointer.next -> value);
return 0;
}
Output:
1
2