0
点赞
收藏
分享

微信扫一扫

OC之【c语言结构体】

<stdio.h>
void
// 这个机构只能在函数内部使用
// 定义一个名为Student的结构体类型
struct
int age; // 年龄
char *name; // 姓名
float height; // 身高
};

// 定义一个结构体变量
struct Student stu = {27, “aa”, 1.8f};
// 下面这行的初始化是错误的
// stu = {27, "aa", 18.0f};

age = 28;

printf("age=%d\n", stu.age);
printf("name=%s\n", stu.name);
printf("height=%.1f\n", stu.height);
}

void
struct
int age; // 年龄
char *name; // 姓名
float height; // 身高
27, "aa", 1.8f};

struct Student stu1 = {28, "laa", 1.9f};


struct
int age; // 年龄
char *name; // 姓名
float height; // 身高
27, "aa", 1.8f};

struct
int age; // 年龄
char *name; // 姓名
float height; // 身高
27, "aa", 1.8f};
}

void
// 定义一个Date结构体
struct
int
int
int
};

// 定义一个学生结构体
struct
int
struct Date
};

struct Student stu = {27, {2009, 10, 10}};

printf("age=%d\n", stu.age);
printf("year=%d\n", stu.birthday.year);
printf("month=%d\n", stu.birthday.month);
printf("day=%d\n", stu.birthday.day);
}

void
// struct Student {
年龄
姓名
身高
// };
// struct Student a[2] = {{27, "aa", 1.8f}, {28, "laa", 1.9f}};

struct
int age; // 年龄
char *name; // 姓名
float height; // 身高
2] = {{27, "aa", 1.8f}, {28, "laa", 1.9f}};

struct Student a2[4];
}


struct
int
};

void change(struct Person
age = 9;
}
// 结构体作为函数参数
void
struct Person person = {27};
change(person);

printf("age=%d", person.age);
}

// 指向结构体的指针
void
// 定义一个结构体变量
struct Person person = {27};
// 定义一个指向结构体的指针
struct Person
// 让指针p指向结构体person
p = &person;

printf("age=%d\n", person.age);
printf("age=%d\n", (*p).age);
printf("age=%d\n", p->age);
}

int main(int argc, const char
{
tets5();
return 0;
}

举报

相关推荐

0 条评论