C语言 - 结构体指针

结构体指针

1 - 代码示例

① 结构体指针和结构体数组

复制代码
 1 #include <stdio.h>
 2 #include<string.h>
 3 // 定义两个结构体
 4 typedef struct student{
 5     
 6     char name[20];
 7     char sex;
 8     int   age;
 9     float score;
10 }stu; // stu 是结构体名,不是结构体变量
11 
12 
13 typedef struct cpoint{
14     
15     float x;
16     float y;
17     
18 }Cpoint;
19 
20 int main(int argc, const char * argv[]) {
21     
22     // 结构体指针
23     stu a = {"rose",'m',23,235.9}; // a 是结构体变量
24     stu *p = &a;//  p 就是结构体指针
25     
26     stu aII ={"asshole",'m',26};
27     *p = aII;// 结构体可以直接赋值
28     // 修改结构体成员变量
29     (*p).sex= 'f';// 方式一
30     p->age = 30;  // 方式二
31     strcpy((*p).name, "jack");// 方式三
32     printf("%s %c %d\n",(*p).name,(*p).sex,(*p).age);// jack f 30
33     
34     
35     // 结构体指针和结构体数组
36     stu b[4]={
37         {"亚洲",'m',23},
38         {"非洲",'m',34},
39         {"美洲",'f',32},
40         {"大洋洲",'m',32}
41     };
42     
43     stu *per0 = &b[0];// 同 stu *p = b;    因为数组名代表数组的首地址
44     printf("%s\n",(*per0).name);// 亚洲
45     
46     stu *per2 = &b[2];// 指向下标为 2 的数据元素
47     printf("%s\n",per2->name);// 美洲
48     
49     Cpoint m[3] ={1.0,2.0};
50     Cpoint n[3] ={3.0,4.0};
51     Cpoint *pl= m;
52     Cpoint *ql = n;
53     float a1 = (pl->x - (*ql).x)*(pl->x - ql->x)+(pl->y - ql->y)*((*pl).y - ql->y);// (1.0 - 3.0) * (1.0 - 3.0) + (2.0-4.0) * (2.0 - 4.0)
54     printf("%.2f\n",a1);// 8.00
55     
56     
57     // 利用指针遍历结构体
58     stu student = {"Mrs.W",'m',16};
59     stu *ps = &student;
60     
61     int i = 0;
62     while (*(ps->name+i)!='\0') {
63         
64         if (*(ps->name+i)=='W') {
65             *(ps->name+i) = 'w';// 大写 W 变换成小写 w
66         }
67         i++;
68     }
69     printf("%s %c %d\n",ps->name,ps->sex,ps->age);// 输出:Mrs.w m 16
70     
71     return 0;
72 }
复制代码

② 把性别为 m 的同学分数全部 +10,若相加后分数存在超 100 的按照 100 对待

复制代码
 1 #include <stdio.h>
 2 
 3 int main(int argc, const char * argv[]) {
 4 
 5     // 在主函数内部声明结构体
 6     typedef struct person{
 7         char names[20];
 8         char sex;
 9         int age;
10         float score;
11     }pon;
12 
13     pon pornA[4]={
14         {"Japen",'m',23,92},
15         {"England",'m',34,89},
16         {"America",'f',32,93},
17         {"China",'m',32,78}
18     };
19 
20     pon *pornB  =  pornA;// 赋值
21 
22     for (int i =0; i<4; i++) {
23         if ((pornB[i].sex)=='m') {
24 
25             (pornB+i)->score +=10;
26             if ((pornB+i)->score>100) {
27                 (*(pornB+i)).score=100;
28             }
29         }
30     }
31 
32     for (int i=0; i<4; i++) {
33         printf("%s %c %d %.2f\n",pornB[i].names,pornB[i].sex,pornB[i].age,pornB[i].score);
34     }
35     
36     return 0;
37 }
复制代码

日志信息

 

posted on   低头捡石頭  阅读(41)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示