| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 515 人关注过本帖
标题:linux gcc 无头链表 段错误
只看楼主 加入收藏
shanhuaiyu
Rank: 1
等 级:新手上路
帖 子:6
专家分:8
注 册:2015-10-12
结帖率:50%
收藏
已结贴  问题点数:30 回复次数:2 
linux gcc 无头链表 段错误
#ifndef     LIST_H__
#define     LIST_H__                                         //声明部分    .h
#define  NAMESIZE  32
struct student
{
        char name[NAMESIZE];
        int num;
        int math;
        int chinese;
};
struct list         //建立链表头节点
{
        struct student data;
        struct list *next;
};
int zhengjia(struct list **,struct student *data);
int show(struct list *);
int chaxun(struct list *,int num);
#endif

#include <stdio.h>                                                                 //   声明.c文件部分
#include <stdlib.h>
#include "list.h"
int zhengjia(struct list **pre,struct student *data)
{
        struct list *new;
        if(new == NULL)
                return -1;
        new = malloc(sizeof(*new));
        new -> next = *pre;
        new -> data = *data;
        *pre = new;
        return 0;
}
int show(struct list *pre)
{
        struct list *head;
        for(head = pre; head != NULL;head = head -> next)
        {
                printf("%s %d %d %d\n",head -> data.name,head -> data.num,head -> data.math,head -> data.chinese);
        }
}
int chaxun(struct list *pre,int num)
{
        struct list *head;
        for(head = pre;head != NULL;head = head -> next)
        {
                if(head -> data.num == num)
                        printf("%s %d %d %d\n",head -> data.name,head -> data.num,head -> data.math,head -> data.chinese);
                        return 0;
        }
        return -1;
}
void shifang(struct list *pre)
{
        struct list *head;
        for(head = pre;head != NULL;head = pre)
        {
                pre = head -> next;
                free(head);
        }
}





#include <stdio.h>
#include <stdlib.h>                                               //main.c部分,只能执行到show部分,
                                                                                   //chaxun(),有错
#include "list.h"                                                      //返回整形,不执行,也不报错
int main()                                                                 //返回struct student  *类型,就报段错误
{
        struct list *pre = NULL;
        struct student tmp,*js;
        int i,ret,num = 2;
        for(i = 0;i < 7 ;i++)
        {
                snprintf(tmp.name,NAMESIZE,"stu%d",i);
                tmp.num = i;
                tmp.math = rand() % 100;
                tmp.chinese = rand() % 100;
                ret = zhengjia(&pre,&tmp);
                if(ret !=0)
                        exit(1);
        }            
        show(pre);
        printf("\n\n");

        chaxun(pre,num);

        shifang(pre);

        exit(0);
}
搜索更多相关主题的帖子: include linux 
2015-10-26 03:46
Spy001
Rank: 9Rank: 9Rank: 9
来 自:山那边
等 级:贵宾
威 望:10
帖 子:138
专家分:1360
注 册:2015-1-23
收藏
得分:15 
int zhengjia(struct list **,struct student *data);
int chaxun(struct list *,int num);

在.h文件里的这两个封装函数后面的参数名可以不要的,只要类型。
我的软件打不开了,没调试过,也看不出来哪里有问题,你可以用gdb调试看看

千百年之后,谁又还记得谁........
2015-10-26 23:15
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:15 
struct list *new;
if(new == NULL)
    return -1;
这是什么意思呀?
2015-10-27 08:35
快速回复:linux gcc 无头链表 段错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019920 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved