1.为什么有结构类型?
c语言中有很多数据类型:整型,浮点,字符,等等。这些类型都是单一的一种类型,如果我们想把一个人的身高,体重,年龄作为一个整体保存该怎么办呢?因此就产生了结构类型。
struct people
{
char* name;
int year;
double weight;
double height;
};/*结尾的分号是初学者最容易忽略的*/
struct people p1;
p1.name = "lufei";
p1.year = 20 ;
p1.weight = 60;
p1.height = 175;
2.声明结构类型的三种方式
第一种:
struct structName
{
double weight;
double height;
};
struct structName name, name2, name3;
第二种:
person1 和 person2 都没有结构类型名称。
struct
{
double weight;
double height;
}person1, person2;
第三种:
最常见的方式
struct structName
{
double weight;
double height;
}person1, person2;
3.结构的初始化,没有给结构成员赋值则默认为0。
#include<stdio.h>
struct people
{
double weight;
double height;
};
int main(int argc, char const *argv[])
{
//第一种初始化方法,按照从上到下的顺序给值。
struct people p1 = {10,10};
//第二种初始化方法,可以指定结构成员赋值。
struct people p2 = {.weight = 20, .height = 20};
printf("%f",p2.height);
return 0;
}
4.结构体的赋值
//第一种给结构体赋值方法
p1.height = 30;
p1.height = 30;
//第二种给结构题赋值方法(强制转换成我们定义的结构)
p1 = (struct people){40,40};
5.注意事项
结构体之间可以相互赋值,
p1 = p2相当于
p1.height = p2.heigh;
p1.weight = p1.weight;
而且p1和p2完全是两个变量,不是指向一个变量的地址。结构变量的名字不像数组,数组的名字是指向地址,结构体的名字就是名字,需要使用&取地址符来获得结构体的地址。
struct people{
double height;
double weight;
}p;
struct people *person = &p;
为了简写:
(*person).height 可以写成 person->height
6.c语言是传值的,如果我们在一个函数的形参放一个结构体,那么我们在函数内部得到的结构体是从外面传来的值,而不是外面那个结构体本身。
#include<stdio.h>
struct people
{
double weight;
double height;
};
void func(struct people p);
int main(int argc, char const *argv[])
{
struct people p1 = {10,10};
func(p1);
printf("在main()函数中");
printf("p.height = %f- p.weight = %f",p1.height,p1.weight);
}
void func(struct people p)
{
p.height = 20;
p.weight = 20;
//scanf("%f %f",&p.height, &p.weight);
printf("在函数fun()中");
printf("p.height = %f- p.weight = %f",p.height,p.weight);
printf("\n");
}
运行结果为:
在函数fun()中p.height = 20.000000- p.weight = 20.000000
在main()函数中p.height = 10.000000- p.weight = 10.000000
由此可以看出,c语言是传值的,我们在函数中的形式参数只能得到一个和main函数中值一样的一个结构体,而不能的到main函数中的那个结构体。(现在p1 和 p完全是两个东西,只不过这两个东西名字相等)
如果想在fun()内部修改main()中的结构体有两种解决办法:
解决方法一:返回一个结构体。返回一个结构体,让main函数中(外部的)结构体 = func()函数返回的的结构体。但是这种方法太费时间和空间,如果结构体很大,电脑则需要重新开辟块同样大的内存。不建议这样做。
解决方法二:传指针
#include<stdio.h>
/*声明一个结构体*/
struct people
{
double weight;
double height;
};
void func(struct people *p);
/*main函数(外函数)*/
int main(int argc, char const *argv[])
{
struct people p1 = {10,10};
func(&p1);
printf("在main()函数中");
printf("p.height = %f- p.weight = %f",p1.height,p1.weight);
}
/*func()内函数*/
void func(struct people *p)
{
p->height = 20;
p->height = 20;
//scanf("%f %f",&p.height, &p.weight);
printf("在函数fun()中");
printf("p.height = %f- p.weight = %f",p->height,p->weight);
printf("\n");
}
输出结果:
在函数fun()中p.height = 20.000000- p.weight = 10.000000
在main()函数中p.height = 20.000000- p.weight = 10.000000
此时两个函数中的结构体都指的是main中的p1(现在p1和p是一个东西,只不过名字不一样)
6.结构数组,与整型数组,浮点数组一样,结构也有结构数组。
#include<stdio.h>
int main(int argc, char const *argv[])
{
struct people
{
int weight;
int height;
};
//声明一个结构数组
const int arraySize = 2;
struct people peopleArray[arraySize] = { { 10, 10 }, { 20, 20 } };
for(int i = 0; i < arraySize; i++)
{
printf("weight = %d - height = %d\n",peopleArray[i].weight, peopleArray[i].height);
}
return 0;
}
7.结构中的结构
结构不仅支持包括一些整型、浮点型等基本数据类型,结构中也可以包括结构。
#include<stdio.h>
int main(int argc, char const *argv[])
{
struct boy
{
double height;
double weight;
};
struct gril
{
double height;
double weight;
};
//结构中的结构,学生包括男孩和女孩。
struct student
{
struct boy boyOne ;
struct gril grilOne;
}s;
//两种赋值方式
s = (struct student){ {10, 10}, {20, 20}};
s.boyOne.height = 30;
s.boyOne.weight = 30;
printf("boyOne's height = %f- boyOne's weight = %f",s.boyOne.height,s.boyOne.weight);
return 0;
}
注意事项:
struct boy
{
double height;
double weight;
};
struct gril
{
double height;
double weight;
};
//结构中的结构,学生包括男孩和女孩。
struct student
{
struct boy boyOne ;
struct gril grilOne;
}s;
//声明一个指向student的指针变量。
struct student *sPoint;
//可以使用下面的方法访问嵌套结构
sPoint->boyOne.height;
//等价于
(*sPoint).boyOne.height;
//但是不能写成下面的样子,因为boyOne不是一个指针
//sPoint->boyOne->heigh;