This is my First post on StackOverFlow.
I was working on linklist – below is my code.
I am just adding a single node to list and printing it — All I am doing is passing a pointer to the “addTermNode” function and then pointing this passed pointer to the newly created Node.
#include<stdio.h>
#include<time.h>
typedef struct _termination_code_ {
int terminationCode;
unsigned long time;
struct _termination_code_ *next;
}termination_code;
int addTermCode(termination_code *infoTerm, int termCode, unsigned long timerInfo)
{
termination_code *node;
node=(termination_code*)malloc(sizeof(termination_code));
if(NULL == node) return -1;
node->terminationCode=termCode;
node->time=timerInfo;
node->next=NULL;
infoTerm = node;
return 0;
}
int main ()
{
termination_code *list2=NULL;
//Add A single node and print it.
if(addTermCode(list2, 12, time(0))==0)
printf("All OK node added\n");
else
printf("something went wrong\n");
printf("Entered info :%d %ld\n",list2->terminationCode,list2->time);
}
Here what I get the output — Not sure why. Please Help.
[zahmed@build3 rnd]$ ./a.out
All OK node added
Segmentation fault
[zahmed@build3 rnd]$
Thanks
The problem is the way you are passing back the new object. Assigning the new object to the pointer will not work the way you have it written. You should return the object instead from your addTermCode() function.
Basically your list2 pointer is still null. Return the newly created object from that function and assign it to list2.
If not, you’ll need to adjust your code so that the pointer is properly assigned.