C语言的核心部分都说得七七八八了,相信你们已经对C语言的基本数据类型(char\int\float)、数组、指针都很熟悉了,今天来学习C语言中另一种数据类型:结构体。在iOS开发中,结构体是常常用到的数据类型,使用频率不亚于指针,因此须要重视,不过用法很是简单。html
* 在第八讲的时候已经介绍了C语言中的数组,用法跟其余语言差很少。当一个总体由多个数据构成时,咱们能够用数组来表示这个总体,可是数组有个特色:内部的每个元素都必须是相同类型的数据。ios
* 在实际应用中,咱们一般须要由不一样类型的数据来构成一个总体,好比学生这个总体能够由姓名、年龄、身高等数据构成,这些数据都具备不一样的类型,姓名能够是字符串类型,年龄能够是整型,身高能够是浮点型。数组
* 为此,C语言专门提供了一种构造类型来解决上述问题,这就是结构体,它容许内部的元素是不一样类型的。函数
结构体内部的元素,也就是组成成分,咱们通常称为"成员"。学习
结构体的通常定义形式为:spa
struct 结构体名{ 类型名1 成员名1; 类型名2 成员名2; …… 类型名n 成员名n; };
struct是关键字,是结构体类型的标志。指针
好比,咱们定义一个学生code
struct Student{ char *name; //姓名 int age; // 年龄 float height; // 身高 };
上面定义了一个叫作Student的结构体,共有name、age、height3个成员。呵呵,看到这里是否有点面向对象的味道呢,其实这跟面向对象彻底是两码事,只能说感受有点像。orm
前面只是定义了名字为Student的结构体类型,并不是定义了一个结构体变量,就像int同样,只是一种类型。htm
接下来定义一个结构体变量,方式有好多种。
struct Student{ char *name; int age; }; struct Student stu;
第6行定义了一个结构体变量,变量名为stu。struct和Student是连着使用的。
struct Student{ char *name; int age; } stu;
结构体变量名为stu
struct { char *name; int age; } stu;
结构体变量名为stu
以下作法是错误的,注意第3行
1 struct Student { 2 int age; 3 struct Student stu; 4 };
struct Date{ int year; int month; int day; }; struct Student{ char *name; struct Date birthday; };
注意第9行
struct Student { char *name; int age; }; struct Student stu;
第1~4行并无分配存储空间,当执行到第6行时,系统才会分配存储空间给stu变量。
好比下面的Student结构体:
1 struct Student { 2 char *name; // 姓名 3 int age; // 年龄 4 float height; // 身高 5 };
在16位编译器环境下,一个Student变量共占用内存:2 + 2 + 4 = 8字节。
将各成员的初值,按顺序地放在一对大括号{}中,并用逗号分隔,一一对应赋值。
好比初始化Student结构体变量stu
struct Student { char *name; int age; }; struct Student stu = {"MJ", 27};
只能在定义变量的同时进行初始化赋值,初始化赋值和变量的定义不能分开,下面的作法是错误的:
struct Student stu; stu = {"MJ", 27};
stuct Student{ char *name; int age; }; // 声明结构体变量(PS:有些相似于面向对象的操做,实例化对象,而后访问相关的成员) struct Student stu; // 访问stu的age stu.age = 26;
第9行对结构体的age成员进行了赋值。"."称为成员运算符,它在全部运算符中优先级最高
struct Date { int year; int month; int day; }; struct Student { char *name; struct Date birthday; }; struct Student stu; stu.birthday.year = 1986; stu.birthday.month = 9; stu.birthday.day = 10;
注意第14行之后的代码
struct Student { char *name; int age; }; struct Student stu1 = {"MJ", 27}; // 将stu1直接赋值给stu2 struct Student stu2 = stu1; printf("age is %d", stu2.age);
注意第9行。输出结果为:
跟结构体变量同样,结构体数组也有3种定义方式
struct Student { char *name; int age; }; struct Student stu[5]; //定义1
struct Student { char *name; int age; } stu[5]; //定义2
struct { char *name; int age; } stu[5]; //定义3
上面3种方式,都是定义了一个变量名为stu的结构体数组,数组元素个数是5
struct { char *name; int age; } stu[2] = { {"MJ", 27}, {"JJ", 30} };
也能够用数组下标访问每个结构体元素,跟普通数组的用法是同样的
将结构体变量做为函数参数进行传递时,其实传递的是所有成员的值,也就是将实参中成员的值一一赋值给对应的形参成员。所以,形参的改变不会影响到实参。
#include <stdio.h> // 定义一个结构体 struct Student { int age; }; void test(struct Student stu) { printf("修改前的形参:%d \n", stu.age); // 修改实参中的age stu.age = 10; printf("修改后的形参:%d \n", stu.age); } int main(int argc, const char * argv[]) { struct Student stu = {30}; printf("修改前的实参:%d \n", stu.age); // 调用test函数 test(stu); printf("修改后的实参:%d \n", stu.age); return 0; }
首先在第4行定义了一个结构体类型Student
* 在第18行定义了一个结构体变量stu,并在第22行将其做为实参传入到test函数
输出结果为:,形参是改变了,可是实参一直没有变过
* 每一个结构体变量都有本身的存储空间和地址,所以指针也能够指向结构体变量
* 结构体指针变量的定义形式:struct 结构体名称 *指针变量名
* 有了指向结构体的指针,那么就有3种访问结构体成员的方式
结构体变量名.成员名
(*指针变量名).成员名
指针变量名->成员名
#include <stdio.h> int main(int argc, const char * argv[]) { // 定义一个结构体类型 struct Student { char *name; int age; }; // 定义一个结构体变量 struct Student stu = {"MJ", 27}; // 定义一个指向结构体的指针变量 struct Student *p; // 指向结构体变量stu p = &stu; /* 这时候能够用3种方式访问结构体的成员 */ // 方式1:结构体变量名.成员名 printf("name=%s, age = %d \n", stu.name, stu.age); // 方式2:(*指针变量名).成员名 printf("name=%s, age = %d \n", (*p).name, (*p).age); // 方式3:指针变量名->成员名 printf("name=%s, age = %d \n", p->name, p->age); return 0; }
输出结果:
注:本文转自M了个J的博客。