标签:style blog http color 使用 os strong 数据
---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
结构体内部的元素,也就是组成成分,我们一般称为"成员"。
结构体的一般定义形式为:
1 struct 结构体名{
2
3 类型名1 成员名1;
4
5 类型名2 成员名2;
6
7 ……
8
9 类型名n 成员名n;
10
11 };
struct是关键字,是结构体类型的标志。
1 struct Student {
2 char *name;
3 int age;
4 };
5
6 struct Student stu;
第6行定义了一个结构体变量,变量名为stu。struct和Student是连着使用的。
struct Student {
char *name;
int age;
} stu;
结构体变量名为stu
struct {
char *name;
int age;
} stu;
结构体变量名为stu
将各成员的初值,按顺序地放在一对大括号{}中,并用逗号分隔,一一对应赋值。
比如初始化Student结构体变量stu
1 struct Student {
2 char *name;
3 int age;
4 };
5
6 struct Student stu = {"MJ", 27};
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} };
也可以用数组下标访问每一个结构体元素,跟普通数组的用法是一样的
将结构体变量作为函数参数进行传递时,其实传递的是全部成员的值,也就是将实参中成员的值一一赋值给对应的形参成员。因此,形参的改变不会影响到实参。
1 #include <stdio.h>
2
3 // 定义一个结构体
4 struct Student {
5 int age;
6 };
7
8 void test(struct Student stu) {
9 printf("修改前的形参:%d \n", stu.age);
10 // 修改实参中的age
11 stu.age = 10;
12
13 printf("修改后的形参:%d \n", stu.age);
14 }
15
16 int main(int argc, const char * argv[]) {
17
18 struct Student stu = {30};
19 printf("修改前的实参:%d \n", stu.age);
20
21 // 调用test函数
22 test(stu);
23
24
25 printf("修改后的实参:%d \n", stu.age);
26 return 0;
27 }
输出结果为:
,形参是改变了,但是实参一直没有变过
指向结构体的指针,3种访问结构体成员的方式
---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
详细请查看:www.itheima.com
黑 马 程 序 员_视频学习总结<C语言>----05 结构体,布布扣,bubuko.com
黑 马 程 序 员_视频学习总结<C语言>----05 结构体
标签:style blog http color 使用 os strong 数据
原文地址:http://www.cnblogs.com/fqgl/p/3872078.html