读书人

请问兄弟们!为什莫老是出现null poin

发布时间: 2012-02-07 17:45:36 作者: rapoo

请教兄弟们!为什莫老是出现null pointer assignment?!
题目如下 利用无名变量计算三角形面积

我的程序如下
#include "stdio.h "
#include "stdlib.h "
#include "math.h "
main()
{
int *p,n,p1;
long s;
p=(int *)malloc (3*sizeof(int));
if (p==NULL)
{
printf ( "you are wrong! ");
exit(0);
}

for (n=0;n <=2;n++)
{
printf ( "please input a number: \n ");
scanf ( "%d ",p[n]);
}
if (p[0]+p[1]> p[2]||p[2]+p[1]> p[0]||p[0]+p[2]> p[1])
{
p1=(p[0]+p[1]+p[2])/2;
s=sqrt(p1*(p1-p[0])*(p1-p[1])*(p1-p[2]));
}
printf ( "%ld ",s);
free (p);

}

能正常运行 但是结果相当大 并且还有null pointer assignment ,请问各位出错在那里了?

[解决办法]
if (p[0]+p[1]> p[2]||p[2]+p[1]> p[0]||p[0]+p[2]> p[1])
条件错了!是&&不是||

读书人网 >C语言

热点推荐