这是我在StackOverFlow上的第一篇文章。 我正在研究链接列表 - 下面是我的代码。 我只是添加一个节点来列出并打印它 - 我所做的只是传递一个指向“addTermNode”函数的指针,然后将此传递的指针指向新创建的节点。C中的单连接列表
#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);
}
这里我得到的输出 - 不知道为什么。请帮忙。
[[email protected] rnd]$ ./a.out
All OK node added
Segmentation fault
[[email protected] rnd]$
感谢
谢谢ricosrealm - 我刚才弄清楚同样的事情.. :) – Ahmed