I am learning C and I am playing with malloc and free. But for some reason when I use free() in the main everything works but when I put it in my function it does not
#include <stdlib.h>
#include <stdio.h>
struct st {
int number;
};
void del(struct st *s) {
if (s == NULL) return;
free(s); s = NULL;
}
int main() {
struct st *s;
s = (struct st *)malloc(sizeof(struct st));
s->number = 4;
printf("The value is: %d", s->number);
del(s);
// free(s); s= NULL;
if(s == NULL) printf("\nThe struct is removed from memory\n");
else printf("\nThe value is: %d\n", s->number);
return 0;
}
This echo:
The value is: 4
The value is: 0
But if I do:
// del(s);
free(s); s= NULL;
it works