问题描述
- C语言,编译和链接均未报错,运行时出现异常。
-
编译器报告异常信息(http://img.ask.csdn.net/upload/201508/30/1440920346_80190.png)代码如下
#include
#include
#include
#define TRUE 1
#define FASLE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef struct elemType
{
float coef;
int expn;}ElemType;
typedef struct polyn
{
ElemType e;
struct polyn *next;
}Polyn;void initPolyn(Polyn *Pn);
Status initList(Polyn *Pn);
int listLength(Polyn Pn);
Status appendList(Polyn *Pn,ElemType e);Status initList(Polyn *Pn)
{
Pn = (Polyn *)malloc(sizeof(Polyn));
Pn->next = NULL;
if(!Pn)
exit(OVERFLOW);
return OK;
}
int listLength(Polyn Pn)
{Polyn *p = Pn.next; int i = 0; printf("function : the length of the PolynList!n"); printf("coef=%f,expn=%dn",Pn.next->e.coef,Pn.next->e.expn); while(p!=NULL) { i++; p = p->next; } return i;
}
Status appendList(Polyn *Pn,ElemType e)
{
Polyn *s = (Polyn *)malloc(sizeof(Polyn));
s->next = NULL;
s->e.coef = e.coef;
s->e.expn = e.expn;
s->next = Pn->next;
Pn->next = s;
return OK;}
void initPolyn(Polyn *Pn)
{
ElemType e;
int i=0,n;
printf("请输入多项式Polyn的项数:");
scanf_s("%d",&n);
printf("请输入多项式Polyn的系数和指数,用逗号隔开!n");
for(i=0;i<n;i++)
{
scanf_s("%f,%d",&e.coef,&e.expn);
appendList(Pn,e);
}
}
void main()
{
Polyn Pn;initList(&Pn); initPolyn(&Pn); printf("%dn",listLength(Pn)); system("pause");
}
解决方案
http://download.csdn.net/detail/nanshao3618/2879641
解决方案二:
initList(Pn);之后Pn还是NULL,想要在子函数改变指针要用2级指针。
解决方案三:
#define TRUE 1
#define FASLE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef struct elemType
{
float coef;
int expn;
}ElemType;
typedef struct polyn
{
ElemType e;
struct polyn *next;
}Polyn;
void initPolyn(Polyn *Pn);
Status initList(Polyn **Pn);
int listLength(Polyn Pn);
Status appendList(Polyn *Pn,ElemType e);
Status initList(Polyn **Pn)
{
*Pn = (Polyn *)malloc(sizeof(Polyn));
(*Pn)->next = NULL;
if(!Pn)
exit(OVERFLOW);
return OK;
}
int listLength(Polyn Pn)
{
Polyn *p = Pn.next;
int i = 0;
printf("function : the length of the PolynList!n");
printf("coef=%f,expn=%dn",Pn.next->e.coef,Pn.next->e.expn);
while(p!=NULL)
{
i++;
p = p->next;
}
return i;
}
Status appendList(Polyn *Pn,ElemType e)
{
Polyn *s = (Polyn *)malloc(sizeof(Polyn));
s->next = NULL;
s->e.coef = e.coef;
s->e.expn = e.expn;
s->next = Pn->next;
Pn->next = s;
return OK;
}
void initPolyn(Polyn *Pn)
{
ElemType e;
int i=0,n;
printf("请输入多项式Polyn的项数:");
scanf_s("%d",&n);
printf("请输入多项式Polyn的系数和指数,用逗号隔开!n");
for(i=0;i<n;i++)
{
scanf_s("%f,%d",&e.coef,&e.expn);
appendList(Pn,e);
}
}
void main()
{
Polyn *Pn;
initList(&Pn);
initPolyn(Pn);
printf("%dn",listLength(*Pn));
system("pause");
}
解决方案四:
我这边可以,你输入的是什么?
我输入1 2,3也没问题,你确定复制了?
解决方案六:
http://blog.csdn.net/lanseshenhua/article/details/5451798