结构体类型的创建

来源:互联网 发布:中国禁枪怕被推翻 知乎 编辑:程序博客网 时间:2024/06/03 19:01

结构体类型的创建

#include "stdafx.h"#if 0一、有名结构体的俩种定义方式:(1)定义类型的同时,完成了变量的定义//struct Student 相当于创建了类型,相当于intstruct Student{char name[100];char sex;int age;float score;}stu; //既然 struct Student为类型则 stu为变量(2)先有类型,再声明/定义变量struct Student{char name[100];char sex;int age;float score;}struct Student s;//优点:一次定义,多处使用,会带来新的命名(提高复杂度)二、无名结构体(1)定义类型的同时,完成了变量的定义struct {char name[100];char sex;int age;float score;}stu;//优点:无名结构体不引入新的类型(降低复杂度)三、别名结构体(1)定义结构体类型的别名typedef struct Student  //加上student方便以后追溯但可以省略{char name[100];char sex;int age;float score;}STU; //此时的STU为结构体类型的别名,而不为变量#endifint _tmain(int argc, _TCHAR* argv[]){//printf("%s\n", s.name);//printf("%s\n", s.sex);return 0;}


原创粉丝点击