c语言 急急急 为什么会出错

来源:百度知道 编辑:UC知道 时间:2024/07/02 05:29:18
#include"stdio.h"
#include"stdlib.h"
struct Item
{
double coef;
int exp;
};
Item Add1(Item left, Item right);
Item* Add2(Item left, Item right);
Item* Add3(Item left, Item right);
bool Add4(Item left, Item right, Item result);
bool Add5(Item left, Item right, Item* pResult);
bool Add6(Item left, Item right, Item* pResult);
bool Add7(Item left, Item right, Item& result);
int main()
{

Item a,b,c;
Item* d;
a.coef = 1.0;
a.exp = 2;
b.coef = 3.0;
b.exp = 4;
c.coef = 5.0;
c.exp = 6;
//d.coef = 7.0;
d->coef =7.0;
d->exp = 8;
Add1(a,b);
Add2(a,b);
Add3(a,b);
Add4(a,b,c);
//Add5(a,b,d);
//Add6(a,b,d);
Add7(a,b,c);
return 0;
}

Item Add1(Item left, Item right)
{
Item result;
result.coef = left.coef + right.coef;
return result;

d=(Item *)malloc(sizeof(Item));你没有为指针d申请空间,你再试下就行了;这句话放在Item*d后

1)
d 没有初始化

2)
//你自己写的函数,下面一个写得还可以,这个应该是粗心
Item* Add2(Item left, Item right)
{
Item result; //局部变量,你应该为其开辟空间
result.coef = left.coef + right.coef;
return &result; //返回局部变量的地址
}

甘难都某奖励

呵呵~好吝啬~要请教就得付出点利益