在c语言中struct的作用,使用C语言中的关键字struct(结构)

7222e40fa8d1f5648239f382eafb96a1.gif

在iOS开发中,结构是常用的数据类型,它们与指针一样频繁使用,因此需要认真对待.

当一个整体由多个数据组成时,我们可以使用数组来表示整体,但是该数组具有一个特征: 内部的每个元素必须是相同类型的数据. 但是,在实际应用中,我们通常需要根据不同类型的数据形成一个整体. 例如,整个学生的身体可以由姓名,年龄和身高等数据组成. 这些数据具有不同的类型. 名称可以是字符串类型或年龄可以是整数类型,高度可以是浮点类型. 为此,C语言专门提供了一种构造类型来解决上述问题,即结构,它允许内部元素具有不同的类型.

结构内部的元素(即组件)通常称为“成员”.

该结构的一般定义形式为:

struct 结构体名{

类型名1 成员名1;

类型名2 成员名2;

……

类型名n 成员名n;

};

例如,我们定义一个学生

struct Student{

char*name;//姓名

int age;//年龄

float height;//身高

};

上面定义了一个名为Student的结构,共有3个成员: 姓名,年龄和身高. 哈哈,看看这里是否有一些面向对象的风格. 实际上结构体,这与面向对象完全不同. 只能说感觉有点相似.

前面只是定义了一个名为Student的结构类型,而不是像int一样的结构变量,只是一个类型.

接下来,有很多方法来定义结构变量.

struct Student{

char*name;//姓名

int age;//年龄

float height;//身高

};

struct Student stu;

87a43a67def813ede4dcd6b4cfe854fe.gif

定义一个结构变量结构体,变量名是stu. struct和Student一起使用.

struct Student{

char*name;//姓名

int age;//年龄

float height;//身高

}stu;

结构变量名称为stu

struct {

char*name;//姓名

int age;//年龄

float height;//身高

}stu;

结构变量名称为stu

以下方法是错误的,请注意第3行

struct Student {

int age;

struct Student stu;

};

struct Date {

int year;

int month;

int day;

};

struct Student {

char *name;

struct Date birthday; //................(1)

};

注意: (1)行

struct Student {

char *name;

int age;

};

// ................在此之前,系统未分配存储空间

5d8c725df56b6d3639b62d691c652647.gif

struct Student stu; // .........执行6行时,系统将为stu变量分配存储空间.

例如,以下学生结构:

struct Student {

char *name; // 姓名

int age; // 年龄

float height; // 身高

};

在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};

struct Student {

char *name;

int age;

};

struct Student stu;

stu.age = 27; // 访问stu的age成员........(2)

The

01300000994862128992086011271_s.png

(2)行为结构的年龄成员分配一个值. “. ”被称为成员运算符,在所有运算符中具有最高优先级

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;

struct Student {

char *name;

int age;

};

struct Student stu1 = {"MJ", 27};

struct Student stu2 = stu1; // 将stu1直接赋值给stu2

printf("age is %d", stu2.age);

输出为: 年龄为27

像结构变量一样,有3种方法来定义结构数组

(1),

struct Student {

char *name;

int age;

};

struct Student stu[5]; //定义1

(2),

struct Student {

char *name;

int age;

} stu[5]; //定义2

(3),

struct {

char *name;

int age;

} stu[5]; //定义3

94eb767d0fcf1eefcb697f3ea860f167.gif

以上三种方法都定义了一个结构体数组,其变量名为stu,数组元素个数为5

struct {

char *name;

int age;

} stu[2] = { {"MJ", 27}, {"JJ", 30} };

您还可以使用数组下标访问每个结构元素,这与常规数组用法相同

当将结构变量作为函数参数传递时,实际上将传递所有成员的值,即,将实际参数中成员的值分配给相应的形式参数成员. 因此,形式参数的更改不会影响实际参数.

554d5f4c98efe0cd77782e7d2e347dbf.png

96E7DE2F-5EFE-4B92-A0A4-EE196E126A3D.png

(1)结构变量名称. 会员名

(2),(*指针变量名称). 会员名

(3)指针变量名称->成员名称

2cb99563379a994477b732ed3e7a3375.png

38622765-4312-4EEE-8D42-148230B72355.png

输出为:

2cf789deaa5e9ec30c1663f1eece4ac0.png

24170725-fe1c21f2d61e4db1a3ea18223098c21b.png

本文来自电脑杂谈,转载请注明本文网址:

http://www.pc-fly.com/a/jisuanjixue/article-171817-1.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值