#include <stdio.h>
struct Airplane {
int lunzi;
int speed;
};
void remake(struct Airplane * air);
// 结构体指针 & 结构体嵌套使用
// 结构体不可以嵌套自己变量(可以嵌套其他结构体的变量), 可以嵌套指向自己这种类型的指针.
int main(int argc, const char * argv[])
{
struct Car {
int lunzi;
int speed;
} car;
struct Car * p = NULL;
car = (struct Car){ 4, 100 };
p = &car;
// 方法1:(*p) 取出结构体, 再用结构体访问里面的内容
printf("lunzi = %d , speed = %d\n", (*p).lunzi, (*p).speed);
// 方法2: p->lunzi 直接访问
printf("lunzi = %d , speed = %d\n", p->lunzi, p->speed);
//************************ stone ***
// 表示月份的时候有用
printf("%02d\n",2);
struct Time {
int hour;
int minute;
int second;
};
struct Calendar {
int year;
int month;
int day;
struct Time time;
};
struct Time time = {03,56,55};
struct Calendar cal = {2016,1,1,time};
printf("%d年%02d月%02d日 %02d时%02d分%02d秒\n",cal.year,cal.month,cal.day,cal.time.hour,cal.time.minute,cal.time.second);
// 嵌套自己的指针
struct Person {
char * name;
int age;
struct Person * child;
};
struct Person xiaofangzi = {"房祖名",28,NULL};
struct Person jackiechan = {"成龙",55,&xiaofangzi};
printf("%s\n",jackiechan.child -> name);
//************************ stone ***
struct Airplane air = {4,500};
remake(&air);
printf("%d\n",air.lunzi);
return 0;
}
// 值传递 是 赋值, 指针传递 才能指向同一个地址, 才能修改.
void remake(struct Airplane * air)
{
air ->lunzi = 8;
}