|
|
问题出在main中,连前面的一起贴出来- #include <stdio.h>
- #include <malloc.h>
- #define TRUE 1
- #define FALSE 0
- #define OK 1
- #define ERROR 0
- #define INFEASIBLE -1
- #define OVERFLOW -2
- #define LIST_INIT_SIZE 100
- #define LISTINCERMENT 10
- #define Status int
- #define ET int
- typedef struct
- {
- ET *elem;
- int length;
- int listsize;
- }List;
- Status InitList(List *L)
- {
- L->elem=(ET *)malloc(LIST_INIT_SIZE*sizeof(ET));
- if(!L->elem)
- exit(OVERFLOW);
- L->length=0;
- L->listsize=LIST_INIT_SIZE;
- return OK;
- }
- Status ListInsert(List *L,int i,ET e)
- {
- int n;
- ET *newbase;
- if(i<1||i>L->length+1)
- return ERROR;
- if(L->length>=L->listsize)
- {
- newbase=(ET *)realloc(L->elem,(L->listsize+LISTINCERMENT)*sizeof(ET));
- if(!newbase)
- exit(OVERFLOW);
- L->elem=newbase;
- L->listsize+=LISTINCERMENT;
- }
- for(n=L->length;n>i-1;n--)
- L->elem[n]=L->elem[n-1];
- L->elem[i-1]=e;
- L->length+=1;
- return OK;
复制代码
main- int main()
- { List *L;
- int i=0;
- InitList(L);
- ListInsert(L,1,i);
- ListInsert(L,1,i);
- ListInsert(L,1,i);
- for(i=0;i<L->length;i++)
- printf("%4d",L->elem[i]);
- printf("\n");
- return OK;
- }
复制代码 出现问题:
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- int main()
- { List *L;
- L=(List *)malloc(sizeof(List));
- int i=0;
- InitList(L);
- ListInsert(L,1,i);
- ListInsert(L,1,i);
- ListInsert(L,1,i);
- for(i=0;i<L->length;i++)
- printf("%4d",L->elem[i]);
- printf("\n");
- return OK;
- }
复制代码 能解决问题
但是main中多定义1个变量,其他不做任何修改- int main()
- { List *L;
- int i=0,k;
- InitList(L);
- ListInsert(L,1,i);
- ListInsert(L,1,i);
- ListInsert(L,1,i);
- for(i=0;i<L->length;i++)
- printf("%4d",L->elem[i]);
- printf("\n");
- return OK;
- }
复制代码 也能解决问题.
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能通过的原因,不胜感激! |
|