Appearance
结构体
概念
开发者可以自定义的数据类型
定义结构体
c
struct House{
char door[50];
char window[50];
char wall[50];
}
使用案例
c
#include <stdio.h>
struct House{
char door[50];
char window[50];
char wall[50];
};
int main ()
{
struct House house = {
"铁门","玻璃窗","红砖墙"
};
printf("这个房子拥有 %s,%s,%s",house.door,house.window,house.wall);
return 0;
}
c
#include <stdio.h>
struct House{
char door[50];
char window[50];
char wall[50];
};
void descriptHouse(struct House house){
printf("这个房子拥有%s,%s,%s\n",house.door,house.window,house.wall);
}
int main ()
{
struct House house1 = {
"铁门","铁窗","红砖墙"
};
struct House house2 = {
"木门","木窗","木板墙"
};
struct House house3 = {
"玻璃门","玻璃窗","黑砖墙"
};
descriptHouse(house1);
descriptHouse(house2);
descriptHouse(house3);
return 0;
}
结构体与指针的应用案例
c
#include <stdio.h>
struct Person{
int health;
int satiety;
};
void toHospital(struct Person *p){
p->health++;
}
void toRestaurant(struct Person *p){
p->satiety++;
}
void feel(struct Person p){
printf("i Feel health is %d , satiety is %d \n",p.health,p.satiety);
}
int main ()
{
struct Person wangDaChui = {50,50};
feel(wangDaChui);
toHospital(&wangDaChui);
toRestaurant(&wangDaChui);
feel(wangDaChui);
return 0;
}