#include#includevoid fun(float *p1,float *p2,float *s){s=(float *)calloc(1,sizeof(float));*s=*p1+*p2++;}int main(){float a[2]={1.1,2.2},b[2]={10.0,20.0},*s=a;fun(a,b,s);printf("%5.2f\n",*s);}为什么输出是1.10,求解fun到底发生了什么
来源:学生作业帮助网 编辑:作业帮 时间:2024/09/09 08:19:29
![#include#includevoid fun(float *p1,float *p2,float *s){s=(float *)calloc(1,sizeof(float));*s=*p1+*p2++;}int main(){float a[2]={1.1,2.2},b[2]={10.0,20.0},*s=a;fun(a,b,s);printf(](/uploads/image/z/2616081-33-1.jpg?t=%23include%23includevoid+fun%28float+%2Ap1%2Cfloat+%2Ap2%2Cfloat+%2As%29%7Bs%3D%28float+%2A%29calloc%281%2Csizeof%28float%29%29%3B%2As%3D%2Ap1%2B%2Ap2%2B%2B%3B%7Dint+main%28%29%7Bfloat+a%5B2%5D%3D%7B1.1%2C2.2%7D%2Cb%5B2%5D%3D%7B10.0%2C20.0%7D%2C%2As%3Da%3Bfun%28a%2Cb%2Cs%29%3Bprintf%28%22%255.2f%5Cn%22%2C%2As%29%3B%7D%E4%B8%BA%E4%BB%80%E4%B9%88%E8%BE%93%E5%87%BA%E6%98%AF1.10%2C%E6%B1%82%E8%A7%A3fun%E5%88%B0%E5%BA%95%E5%8F%91%E7%94%9F%E4%BA%86%E4%BB%80%E4%B9%88)
#include#includevoid fun(float *p1,float *p2,float *s){s=(float *)calloc(1,sizeof(float));*s=*p1+*p2++;}int main(){float a[2]={1.1,2.2},b[2]={10.0,20.0},*s=a;fun(a,b,s);printf("%5.2f\n",*s);}为什么输出是1.10,求解fun到底发生了什么
#include
#include
void fun(float *p1,float *p2,float *s)
{s=(float *)calloc(1,sizeof(float));
*s=*p1+*p2++;
}
int main()
{float a[2]={1.1,2.2},b[2]={10.0,20.0},*s=a;
fun(a,b,s);printf("%5.2f\n",*s);
}
为什么输出是1.10,求解fun到底发生了什么
#include#includevoid fun(float *p1,float *p2,float *s){s=(float *)calloc(1,sizeof(float));*s=*p1+*p2++;}int main(){float a[2]={1.1,2.2},b[2]={10.0,20.0},*s=a;fun(a,b,s);printf("%5.2f\n",*s);}为什么输出是1.10,求解fun到底发生了什么
你认为应该是多少呢?输出是正确的,其值就是a[0],它被初始化为1.1.因为fun中为s指针重新分配了指向,*s=*p1+*p2++;这一句中的s已不再指向a了.因为指针形参本身也是拷贝调用,所以main中的s仍然是指向a的,printf("%5.2f\n",*s);输出自然是a[0].可以这样验证,在fun中的*s=*p1+*p2++;后加一句printf("%5.2f\n",*s);,它就应该输出11.1了……