LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 999|回复: 2

C 问题,百思不得其解.

[复制链接]
发表于 2006-9-26 13:54:47 | 显示全部楼层 |阅读模式
问题出在main中,连前面的一起贴出来
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #define TRUE        1
  4. #define FALSE        0
  5. #define OK        1
  6. #define ERROR        0
  7. #define INFEASIBLE        -1
  8. #define OVERFLOW        -2
  9. #define LIST_INIT_SIZE        100
  10. #define LISTINCERMENT        10
  11. #define Status int
  12. #define ET        int
  13. typedef struct
  14. {
  15.         ET *elem;
  16.         int length;
  17.         int listsize;
  18. }List;
  19. Status InitList(List *L)
  20. {
  21.         L->elem=(ET *)malloc(LIST_INIT_SIZE*sizeof(ET));
  22.         if(!L->elem)
  23.                 exit(OVERFLOW);
  24.         L->length=0;
  25.         L->listsize=LIST_INIT_SIZE;
  26.         return OK;
  27. }
  28. Status ListInsert(List *L,int i,ET e)
  29. {       
  30.         int n;
  31.         ET *newbase;
  32.         if(i<1||i>L->length+1)
  33.                 return ERROR;
  34.         if(L->length>=L->listsize)
  35.         {
  36.                 newbase=(ET *)realloc(L->elem,(L->listsize+LISTINCERMENT)*sizeof(ET));
  37.                 if(!newbase)
  38.                         exit(OVERFLOW);
  39.                 L->elem=newbase;
  40.                 L->listsize+=LISTINCERMENT;
  41.         }
  42.         for(n=L->length;n>i-1;n--)
  43.                 L->elem[n]=L->elem[n-1];
  44.         L->elem[i-1]=e;
  45.         L->length+=1;
  46.         return OK;
复制代码

main
  1. int main()
  2. {        List *L;
  3.         int i=0;
  4.         InitList(L);
  5.         ListInsert(L,1,i);
  6.         ListInsert(L,1,i);
  7.         ListInsert(L,1,i);
  8.         for(i=0;i<L->length;i++)
  9.                 printf("%4d",L->elem[i]);
  10.         printf("\n");
  11.                 return OK;
  12. }
复制代码
出现问题:
cho@cho:~$ gcc -o List List.c
List.c: 在函数 ‘InitList’ 中:
List.c:28: 警告: 隐式声明与内建函数 ‘exit’ 不兼容
List.c: 在函数 ‘ListInsert’ 中:
List.c:44: 警告: 隐式声明与内建函数 ‘exit’ 不兼容
cho@cho:~$ ./List
段错误
是因为没分配L的空间
main
  1. int main()
  2. {        List *L;
  3.          L=(List *)malloc(sizeof(List));
  4.         int i=0;
  5.         InitList(L);
  6.         ListInsert(L,1,i);
  7.         ListInsert(L,1,i);
  8.         ListInsert(L,1,i);
  9.         for(i=0;i<L->length;i++)
  10.                 printf("%4d",L->elem[i]);
  11.         printf("\n");
  12.                 return OK;
  13. }
复制代码
能解决问题


但是main中多定义1个变量,其他不做任何修改
  1. int main()
  2. {        List *L;
  3.         int i=0,k;
  4.         InitList(L);
  5.         ListInsert(L,1,i);
  6.         ListInsert(L,1,i);
  7.         ListInsert(L,1,i);
  8.         for(i=0;i<L->length;i++)
  9.                 printf("%4d",L->elem[i]);
  10.         printf("\n");
  11.                 return OK;
  12. }
复制代码
也能解决问题.
cho@cho:~$ gcc -o List List.c
List.c: 在函数 ‘InitList’ 中:
List.c:28: 警告: 隐式声明与内建函数 ‘exit’ 不兼容
List.c: 在函数 ‘ListInsert’ 中:
List.c:44: 警告: 隐式声明与内建函数 ‘exit’ 不兼容
cho@cho:~$ ./List
   0   0   0

麻烦谁解释第3个main能通过的原因,不胜感激!
发表于 2006-9-26 16:15:10 | 显示全部楼层
L 未分配空间
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-9-26 18:05:14 | 显示全部楼层
哦,问题没问清楚
加上
  1. L=(List *)malloc(sizeof(List));
复制代码
就OK啦,我是想知道第3个main中多定义1个变量k就行了是什么原因.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表