C语言定义结构体:struct 结构体名称{域定义};
但是为了使用方便用:typedef struct {域定义};结构体名称;
示例:
#include<stdio.h>
#include<math.h>
struct Point{double x,y;};
double dist(struct Point a,struct Point b)
{
return hypot(a.x - b.x,a.y - b.y);
}
int main()
{
Point a,b;
a.x = 1,a.y = 1;
b.x = 2,b.y = 2;
double c = dist(a,b);
printf("%lf\n",c);
return 0;
}
#include<stdio.h>
#include<math.h>
typedef struct {double x,y;}Point; //多了typedef,可以像int ,double,float用
double dist(Point a,Point b) //少了struct
{
return hypot(a.x - b.x,a.y - b.y);
}
int main()
{
Point a,b;
a.x = 1,a.y = 1;
b.x = 2,b.y = 2;
double c = dist(a,b);
printf("%lf\n",c); //输出1.414214
return 0;
}