免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1209 | 回复: 4
打印 上一主题 下一主题

请教段错误问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-01 09:30 |只看该作者 |倒序浏览
刚学习编程  在网上找了个数据结构的例子  但编译后运行总是出现段错误  不知其解  希望高手指点怎么调试程序  有时我加一些打印语句跟踪  但在main开始就加打印语句但什么也打印不出来   希望高手指点一下调试心得   不胜感激!   我的源程序是:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
//#include <conio.h>

#define ERROR 0
#define OK 1
#define EQUAL 1

struct STU
{
        char name[20];
        char stuno[10];
        int age;
        int score;
}stu[50];
typedef struct STU ElemType;

struct LIST
{
        ElemType elem[50];
        int length;
};
typedef struct LIST List;

void init(List **L)
{
        *L = (List *)malloc(sizeof(List));
        (*L)->length = 0;
}

int ListLength(List *L)
{
        return L->length;
}

void GetElem(List L,int i,ElemType *e)
{
        *e = L.elem[i];
}

int EqualList(ElemType *e1,ElemType *e2)
{
        if(strcmp(e1->name,e2->name))
                return 0;
        if(strcmp(e1->stuno,e2->stuno))
                return 0;
        if(e1->age != e2->age)
                return 0;
        if(e1->score != e2->score)
                return 0;
        return 1;
}

int LocateElem(List *La,ElemType e,int type)
{
        int i;
        switch(type)
        {
                case EQUAL:
                        for(i=0;i<La->length;i++)
                                if(EqualList(&La

->elem[i],&e))
                                        return 1;
                        break;
                default:
                        break;
        }
        return 0;
}

void UnionList(List *La,List *Lb)
{
        int i;
        int La_len,Lb_len;
        ElemType e;

        La_len = ListLength(La);
        Lb_len = ListLength(Lb);

        for (i=0;i<Lb_len;i++)
        {
                GetElem(*Lb,i,&e);
                if(!LocateElem(La,e,EQUAL))
                        ListInsert

(La,++La_len,e);
        }
}

void PrintList(List L)
{
        int i;
        printf("name                stuno               

age                score\n");
        for(i=0;i<L.length;i++)
                printf("%-10s\t%s\t%d\t%

d\n",L.elem[i].name,L.elem[i].stuno,L.elem

[i].age,L.elem[i].score);
        printf("\n");
}

int ListInsert(List *L,int i,ElemType e)
{
        ElemType *q,*p;
        if(i<1 || i>L->length+1)
                return ERROR;
        q = &(L->elem[i-1]);
        for(p=&(L->elem[L->length-1]);p>=q;p--)
                *(p+1) = *p;
        *q = e;
        ++(L->length);
        return OK;
}

int main()
{
        struct STU e;
        List *La,*Lb;
//        La = NULL;
//        Lb = NULL;

        printf("\n\n----------List Demo is

running...-----------");
        init(&La);
        strcpy(e.name,"zhang");
        strcpy(e.stuno,"1001");
        e.age = 22;
        e.score = 78;
        ListInsert(La,1,e);
        strcpy(e.name,"wang");
        strcpy("e.stuno","1002");
        e.age = 23;
        e.score = 88;
        ListInsert(La,2,e);

        PrintList(*La);
        printf("List A length now is:%d\n",La-

>length);
       
        init(&Lb);
        strcpy(e.name,"li");
        strcpy(e.stuno,"1003");
        e.age = 21;
        e.score = 90;
        ListInsert(Lb,1,e);
        strcpy(e.name,"zhao");
        strcpy(e.stuno,"1004");
        e.age = 19;
        e.score = 66;
        ListInsert(Lb,2,e);

        PrintList(*Lb);

        printf("Union..........\n");
        UnionList(La,Lb);
        printf("Now  is:\n");
        PrintList(*La);
}

论坛徽章:
0
2 [报告]
发表于 2006-08-01 10:37 |只看该作者
注意指针

论坛徽章:
0
3 [报告]
发表于 2006-08-01 12:26 |只看该作者
没看出来,
调了一下,差点晕死。
strcpy("e.stuno","1002");

论坛徽章:
0
4 [报告]
发表于 2006-08-01 15:27 |只看该作者
谢谢楼上
我当时敲错了
请问楼上你用什么工具调试呢?
使用gdb吗?  还是怎么调试的   
谢谢!

论坛徽章:
0
5 [报告]
发表于 2006-08-01 15:41 |只看该作者
linux下一般是使用gdb的。如果需要更多的功能可以网上找一些工具来用。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP