I suspect that you are asking us to help you with your homework, so I am not going to solve all of your code problems for you. I am only going to point out your most egregious mistake, which is the mistake you asked about -- why does the second call to your create_Set()
function fail?
test.c, lines 27-28:
Set* s2;
Set_Create(s2); //.. Here error comes..
In set.c, I have trimmed it down to the meaningful code:
void Set_Create(Set* set){
if(set == NULL){
set = (Set *)malloc(sizeof(Set));
}
set->size = 0;
set->data = -1;
set->next = NULL;
}
What's happening is that both of your calls to Set_Create
are overwriting some arbitrary, unpredictable, memory locations. This is a serious bug.
In the case of set1
, the arbitrary pointer you have been experiencing seems to happen to point to some place in memory which can be written to. Who knows what data you are clobbering, but your data can survive in that spot for a short duration.
In the case of set2
, the arbitrary pointer you are encountering is not a valid memory location for you to modify, so your program crashes.
Change your create_Set
like so:
Set* Set_Create() {
Set * set = calloc(1, sizeof(Set));
set->data = -1;
return set;
}
And in test.c:
Set * set1 = Set_Create();