我是靠谱客的博主 震动飞机,这篇文章主要介绍C语言结构体解析1 小序2 用例3 结构体变量和指向结构体的指针作函数参数4 总结,现在分享给大家,希望可以做个参考。

1 小序

不同数据类型数据的集合;
结构:
struct 结构体名{成员列表};
声明方法:

1.0 先声明结构体类型后定义结构体变量

  • Demo1
复制代码
1
2
3
4
5
6
7
8
9
10
11
struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; // 结构体数组student2[5] struct student student1, student2[5]={{...}, {...}, {...}, {...}, {...}};
  • Demo2
复制代码
1
2
3
4
5
6
7
8
9
10
11
typedef struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; // 结构体数组student2[5] student student1, student2[5]={{...}, {...}, {...}, {...}, {...}};

使用typedef是类型定义,可直接使用定义的变量作为类型名,如student为结构体类型,使用student直接定义结构体变量。

1.2 声明类型同时定义变量

复制代码
1
2
3
4
5
6
7
8
9
struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }student1, student2;

1.3 直接定义结构体类型变量

复制代码
1
2
3
4
5
6
7
8
9
struct { int num; char name[20]; char sex; int age; float score; char addr[30]; }student1, student2;

1.4 指向结构体变量的指针

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
struct { int num; char name[20]; char sex; int age; float score; char addr[30]; }student1={250, "xin",'M', 10, 99.9, "shenzhen"}, *p; // 指针变量p指向结构体变量student1 p = &student1; printf("%s", p->name); printf("%s", (*p).name);

2 用例

2.1 初始化结构体

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { struct people{ int age; char name[20]; char address[20]; char sex; }shui={10, "qingqing", "beijing", 'F'}; struct people xin; struct people tian={ 10, "lanlan", "shenzhen", 'F' }; xin.age = 10; strcpy(xin.name, "xindaqi"); strcpy(xin.address, "shenzhen"); xin.sex = 'M'; printf("name: %s n age: %d n sex: %c n address: %s", xin.name, xin.age, xin.sex, xin.address); printf("name: %s n age: %d n sex: %c n address: %s n", tian.name, tian.age, tian.sex, tian.address); printf("name: %s n age: %d n sex: %c n address: %s n", shui.name, shui.age, shui.sex, shui.address); }
  • Result
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
name: xindaqi age: 10 sex: M address: shenzhenname: lanlan age: 10 sex: F address: shenzhen name: qingqing age: 10 sex: F address: beijing
  • Analysis
    (1) 三种方法初始化结构体变量:直接法,间接法和独立法,其中直接法是在定义结构体时直接定义结构体变量并进行初始化(shui);间接法为先定义结构体,然后定义结构体变量(tian)并进行初始化,独立法在间接法的基础上,分别定义结构体中的变量;
    (2) 初始化字符数组时,有两种有效的方式,统一整体赋值,如shui={10, "qingqing", "beijing", 'F'};,通过strcpy对字符数组赋值;

2.2 指针变量操作结构体变量

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { // printf("hello world!"); struct people{ int age; char name[20]; char address[20]; char sex; }shui={10, "qingqing", "beijing", 'F'}; struct people xin; struct people tian={ 10, "lanlan", "shenzhen", 'F' }; struct people *p; p = &xin; xin.age = 10; strcpy(xin.name, "xindaqi"); strcpy(xin.address, "shenzhen"); xin.sex = 'M'; printf("name: %s n age: %d n sex: %c n address: %s", xin.name, xin.age, xin.sex, xin.address); printf("name: %s n age: %d n sex: %c n address: %s n", tian.name, tian.age, tian.sex, tian.address); printf("name: %s n age: %d n sex: %c n address: %s n", shui.name, shui.age, shui.sex, shui.address); printf("name: %s n age: %d n sex: %c n address: %s n", p->name, p->age, p->sex, p->address); printf("name: %s n age: %d n sex: %c n address: %s n", (*p).name, (*p).age, (*p).sex, (*p).address); system("pause"); }
  • Analysis
    (1) 指向结构体变量的指针直接将变量地址赋给指针变量即可,该指针变量是结构体类型的指针;
    (2) 指针指向结构体后,对结构体数据的操作有两种方式:点(.)操作和指向运算符(->)操作;

2.3 指向结构体数组的指针

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h> #include <stdlib.h> #include <string.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { // printf("hello world!"); struct people{ int age; char name[20]; char address[20]; char sex; }shui={10, "qingqing", "beijing", 'F'}; struct people class_one[2]={ { 10, "xiaoxiao", "guangzhou", 'M' }, { 10, "xiaoer", "shanghai", 'F' } }; struct people *p1; printf("name: %s n age: %d n sex: %c n address: %s n", class_one[0].name, class_one[0].age, class_one[0].sex, class_one[0].address); printf("Name Age Sex Addressn"); for(p1=class_one;p1<class_one+2;p1++){ printf("%-10s %-4d %2c %-20sn", p1->name, p1->age, p1->sex, p1->address); } system("pause"); // return 0; }
  • Result
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
name: xiaoxiao age: 10 sex: M address: guangzhou Name Age Sex Address xiaoxiao 10 M guangzhou xiaoer 10 F shanghai
  • Analysis
    (1) 结构体数组是特别的数组,该数组中的元素为结构体元素,因此数据类型可以一致也可以不一致,扩展了C的数组功能;
    (2) 结构体数组引用和普通数组一致, 如class_one[0]引用第一个元素,该元素存放的是结构体数据,使用数据:class_one[0].name;
    (3) 指向结构体数组的指针,使用指针处理结构体数组数据只需将结构体数组的数组名(数组初始地址)赋予指针变量即可:p1=class_one;;

3 结构体变量和指向结构体的指针作函数参数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h> #include <stdlib.h> #include <string.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ struct people{ int age; char name[20]; char address[20]; char sex; }shui={10, "qingqing", "beijing", 'F'}; int main(int argc, char *argv[]) { void print(struct people *); print(&shui); system("pause"); } void print(struct people *p){ printf("struct pointer as fuction formal parameter!n"); printf("name: %s n age: %d n sex: %c n address: %s n", p->name, p->age, p->sex, p->address); }
  • Result
复制代码
1
2
3
4
5
6
struct pointer as fuction formal parameter! name: qingqing age: 10 sex: F address: beijing
  • Analysis
    (1) 指向结构体的指针作为函数形参,传递形参时将结构体数组的初始地址(数组名)或结构体变量初始地址(&shui)传递给形参(*p);
    (2) 该功能即定义指向结构体的指针,该指针指向结构体变量或结构体数组;

4 总结

(1) 结构体是C语言中存储多类型变量的"自定义变量类型",该变量类型可定义新的变量,新的变量拥有结构体定义数据类型的属性;
(2) 结构体是面向过程语言中的面向对象处理方式,即对不同类型的数据进行封装;
(3) 结构体中的字符数组初始化有两种方式:整体初始化(shui={10, “qingqing”, “beijing”, ‘F’};)和独立初始化(str(xin.name, “xindaqi”); ),独立初始化需要使用复制功能;
(4) 结构体数组是对C数组的扩展,该数组可存放不同类型的数据,指针进行处理时,只需将数组的初始地址赋予指针变量即可;
(5) 结构体指针作为函数形参接受结构体变量地址;


[参考文献]
[1]https://blog.csdn.net/ginwafts/article/details/80858232


最后

以上就是震动飞机最近收集整理的关于C语言结构体解析1 小序2 用例3 结构体变量和指向结构体的指针作函数参数4 总结的全部内容,更多相关C语言结构体解析1内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部