Chinaunix
标题:
求助 段错误
[打印本页]
作者:
flycms
时间:
2011-01-05 20:28
标题:
求助 段错误
#include<stdio.h>
#include<stdlib.h>
#include<setjmp.h>
typedef struct node * Node;
Node head;
jmp_buf env;
struct node{
int val;
Node next;
};
int insert(int val)
{
Node p,q;
p= head;
if(p!=NULL)
{
while(p->next !=NULL)
{p=p->next;}
}
q=(Node)malloc(1*sizeof(struct node));
if(q == NULL) return -1;
q->next = NULL;
q->val = val;
if(p==NULL){
head=q;
return 1;
}
p->next=q;
return 1;
}
void destroy()
{
Node p = head;
while(p!=NULL)
{
Node q;
q=p;
p=p->next;
free(q);
}
head = NULL;
}
void print(Node head)
{
Node p;
p = head;
while(p!=NULL)
{
printf("%s\n",p->val);
p = p->next;
}
}
int main(void){
Node p;
int i;
int res = -1;
res=setjmp(env);
printf("%s\n",res);
if(res !=0 )
goto err;
printf("insert\n");
for(i=1;i<8;i++)
insert(i);
print(head);
res = 0;
err:
destroy();
return res;
}
复制代码
作者:
flycms
时间:
2011-01-05 21:15
上边的print()函数错误
应该是printf("%d\n",p->val);
void print(Node head)
54 {
55 Node p;
56 p = head;
57 while(p!=NULL)
58 {
59 printf("%d\n",p->val);
60 p = p->next;
61 }
62 }
哎,.........可怜的自己
作者:
rain_fish
时间:
2011-01-05 21:37
呵呵,找到问题就好
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2