在学习结构体,使用函数调用的过程中遇到一个有关问题
在学习结构体,使用函数调用的过程中遇到一个问题
编译时出错了:错误如下:
E:\example\C\ComplexStruct.c:28:1: error: incompatible type for argument 1 of 'p
rint_complex'
E:\example\C\ComplexStruct.c:3:6: note: expected 'struct complex_struct' but arg
ument is of type 'struct complex_struct'
有人知道为什么吗?本人菜鸟,求指导,不胜感激!
------解决方案--------------------
#include <stdio.h>
struct complex_struct {double x,y;};
void print_complex(struct complex_struct z)
{
if(z.x==0&&z.y==0)
{
printf("0");
}
else if(z.x==0&&z.y!=0)
{
printf("%.1fi",z.y);
}
else if(z.x!=0&&z.y==0)
{
printf("%.1f",z.x);
}
else
{
printf("%.1f+%fi",z.x,z.y);
}
}
main()
{
struct complex_struct {double x,y;};
struct complex_struct z1;
z1.x=3.0;
z1.y=4.0;
print_complex(z1);
}
编译时出错了:错误如下:
E:\example\C\ComplexStruct.c:28:1: error: incompatible type for argument 1 of 'p
rint_complex'
E:\example\C\ComplexStruct.c:3:6: note: expected 'struct complex_struct' but arg
ument is of type 'struct complex_struct'
有人知道为什么吗?本人菜鸟,求指导,不胜感激!
------解决方案--------------------
#include <stdio.h>
struct complex_struct {double x,y;};
void print_complex(struct complex_struct z)
{
if(z.x==0&&z.y==0)
{
printf("0");
}
else if(z.x==0&&z.y!=0)
{
printf("%.1fi",z.y);
}
else if(z.x!=0&&z.y==0)
{
printf("%.1f",z.x);
}
else
{
printf("%.1f+%fi",z.x,z.y);
}
}
main()
{
struct complex_struct z1;
z1.x=3.0;
z1.y=4.0;
print_complex(z1);
}