struct&typedef函数学习笔记

来源:互联网 发布:知肤泉水光面膜有效期 编辑:程序博客网 时间:2024/05/29 16:14


struct:可以看成是一个自定义的数据类型,可以定义结构体类型所对应的变量

注:1.只要是操作字符串,必须要用字符串的方法,比如如果想改变字符串里的内容,不能使用直接=的方式赋值,还是用strcpy

2.struct自定义类型可以直接传值,数组不可以.

e.g.  

<span style="font-size:18px;">struct Students stu4={25,54.6,93,"王五"};struct Students stu5={29,87.6,23,"刘珊珊"};stu4=stu5;</span>

 

1.struct(结构体)

(1)函数的声明方式:与枚举类型类似

e.g.  

<span style="font-size:18px;color:#333333;">enum {};</span>

结构体的声明方式即为

<span style="font-size:18px;color:#333333;">struct student{//student 为变量名称//成员变量    int stuAge;          float stuScore;     char stuSex;         char stuName[20];};</span>
调用数据类型需写 struct student


(2)调用内部成员变量的方法

<span style="font-size:18px;color:#333333;">student.stuScore;</span>


(3)结构体数组

与声明其他类型数组一样,数据类型为结构体,数组内需要保存的全部是结构体对应的类型


2.typedef的使用环境

使用情景:将数据类型(int,float,struct student,long)等形式转换为另一种写法,另一种表示方式


3.struct&typedef结合使用

此两个函数结合使用可以使代码更加简洁

使用方式一:

<span style="font-size:18px;color:#333333;"></span><pre name="code" class="objc"><span style="font-size:18px;color:#333333;">typedef struct student{//student 为变量名称//成员变量    int stuAge;          float stuScore;     char stuSex;         char stuName[20];}Student;</span>
typedef struct student Student;

使用方式二:

<span style="font-size:18px;color:#333333;">typedef struct student{//student 为变量名称//成员变量    int stuAge;          float stuScore;     char stuSex;         char stuName[20];}Student;</span>

使用此两种方法后,在日后调用结构体时候,均可直接使用Student名称



0 0