码迷,mamicode.com
首页 > 编程语言 > 详细

C语言结构体训练

时间:2018-11-03 01:56:36      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:分享图片   经验   warning   img   long   class   fun   war   clu   

结构体大小和内存结构

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 //结构体需要根据数据类型进行内存对齐
 7 //struct stus
 8 //{
 9 //    char name[20];//20
10 //    unsigned int age;//4
11 //    char tel[15];//15
12 //    char sex;//1  52
13 //    float scores[3];//12
14 //}stu;
15 
16 struct stus
17 {
18     //char * p;   //4
19     //char arr[2];//2  8
20     //short d; //2   16
21     //int c;  //4 
22     //long g;//4    
23     //double f;//8  24
24     //float h[2];//8   40
25 
26 
27     double f;//8  24
28     float h[2];//8   40
29     long g;//4    
30     int c;  //4 
31     char * p;   //4
32     short d; //2   16
33     char arr[2];//2  8
34 
35 
36     /*
37     姓名:  char name[200];
38     等级: int 
39     当前经验:int 
40     攻击: int 
41     防御:int 
42     技能冷却:foat 
43 
44     */
45 
46 
47 }stu;
48 
49 
50 int main()
51 {
52     
53     printf("结构体大小%d\n", sizeof(stu));
54 
55     system("pause");
56     return EXIT_SUCCESS;
57 }

技术分享图片

结构体学生成绩排序

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 struct stu1
 7 {
 8     //成员列表
 9     char name[21];
10     float scores[3];
11 };
12 int main()
13 {
14     struct stu1 s[3];
15     for (int i = 0; i < 3; i++)
16     {
17         printf("请您输入学生 姓名   成绩 :\n");
18         scanf("%s%f%f%f", s[i].name, &s[i].scores[0], &s[i].scores[1], &s[i].scores[2]);
19     }
20     //冒泡排序
21     for (int i = 0; i < 3 - 1; i++)
22     {
23         for (int j = 0; j < 3 - i - 1; j++)
24         {
25 
26             int sum1 = s[j].scores[0] + s[j].scores[1] + s[j].scores[2];
27             int sum2 = s[j + 1].scores[0] + s[j + 1].scores[1] + s[j + 1].scores[2];
28             if (sum1 > sum2)
29             {
30                 //结构体交换   交换所有成员列表中的数据
31                 //交换姓名
32                 char temp[21] = { 0 };
33                 strcpy(temp, s[j].name);
34                 strcpy(s[j].name, s[j + 1].name);
35                 strcpy(s[j + 1].name, temp);
36 
37                 //交换成绩
38 
39                 for (int k = 0; k < 3; k++)
40                 {
41                     float temp = s[j].scores[k];
42                     s[j].scores[k] = s[j + 1].scores[k];
43                     s[j + 1].scores[k] = temp;
44                 }
45 
46             }
47         }
48     }
49 
50 
51 
52 
53     for (int i = 0; i < 3; i++)
54     {
55         printf("姓名:%s\n", s[i].name);
56         printf("成绩: %.1f   %.1f   %.1f\n", s[i].scores[0], s[i].scores[1], s[i].scores[2]);
57     }
58 
59 
60     system("pause");
61     return EXIT_SUCCESS;
62 }

学生成绩优化

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 struct stu1
 7 {
 8     //成员列表
 9     char name[21];
10     float scores[3];
11 };
12 int main()
13 {
14     struct stu1 s[3];
15     for (int i = 0; i < 3; i++)
16     {
17         printf("请您输入学生 姓名   成绩 :\n");
18         scanf("%s%f%f%f", s[i].name, &s[i].scores[0], &s[i].scores[1], &s[i].scores[2]);
19     }
20     //冒泡排序
21     for (int i = 0; i < 3 - 1; i++)
22     {
23         for (int j = 0; j < 3 - i - 1; j++)
24         {
25 
26             int sum1 = s[j].scores[0] + s[j].scores[1] + s[j].scores[2];
27             int sum2 = s[j + 1].scores[0] + s[j + 1].scores[1] + s[j + 1].scores[2];
28             if (sum1 > sum2)
29             {
30                 //结构体交换   交换所有成员列表中的数据
31                 ////交换姓名
32                 //char temp[21] = { 0 };
33                 //strcpy(temp, s[j].name);
34                 //strcpy(s[j].name, s[j + 1].name);
35                 //strcpy(s[j + 1].name, temp);
36 
37                 ////交换成绩
38 
39                 //for (int k = 0; k < 3; k++)
40                 //{
41                 //    float temp=s[j].scores[k];
42                 //    s[j].scores[k] = s[j + 1].scores[k];
43                 //    s[j + 1].scores[k] = temp;
44                 //}
45 
46                 //结构体变量交换
47                 struct stu1 temp = s[j];
48                 s[j] = s[j + 1];
49                 s[j + 1] = temp;
50 
51             }
52         }
53     }
54 
55 
56 
57 
58     for (int i = 0; i < 3; i++)
59     {
60         printf("姓名:%s\n", s[i].name);
61         printf("成绩: %.1f   %.1f   %.1f\n", s[i].scores[0], s[i].scores[1], s[i].scores[2]);
62     }
63 
64 
65     system("pause");
66     return EXIT_SUCCESS;
67 }

结构体成员为指针

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 struct stuinfo
 7 {
 8     char *name;
 9     int age;
10 };
11 
12 int main()
13 {
14     struct  stuinfo si;
15     si.name = (char *)malloc(sizeof(char) * 21);
16 
17     strcpy(si.name, "张三");
18     si.age = 18;
19 
20     printf("%s   %d\n", si.name, si.age);
21 
22     free(si.name);
23 
24 
25     system("pause");
26     return EXIT_SUCCESS;
27 }

技术分享图片

结构体指针

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 struct sinfo
 7 {
 8     char *name;
 9     int age;
10 }stu;
11 int main()
12 {
13     struct sinfo * s = &stu;
14     s->name = (char *)malloc(sizeof(char) * 21);
15     strcpy(s->name, "李芮");
16     s->age = 50;
17     printf("%s   %d\n", s->name, s->age);
18 
19     free(s->name);
20 
21 
22 
23     system("pause");
24     return EXIT_SUCCESS;
25 }

 

 技术分享图片

 

堆空间开辟结构体

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 struct tec
 7 {
 8     char *name;//4
 9     int age;//4
10 }t;
11 
12 int main()
13 {
14     struct tec * p = (struct tec *)malloc(sizeof(t));
15     p->name = (char *)malloc(sizeof(char) * 21);
16 
17     strcpy(p->name, "牛玲");
18     p->age = 18;
19 
20     printf("%s   %d\n", p->name, p->age);
21 
22     if (p->name != NULL)
23     {
24         free(p->name);
25         p->name = NULL;
26     }
27 
28     if (p)
29     {
30         free(p);
31         p = NULL;
32     }
33 
34 
35     printf("%d\n", sizeof(struct tec *));//无论为什么类型的指针,都是4个
36 
37     system("pause");
38     return EXIT_SUCCESS;
39 }

 

 

技术分享图片

 学生成绩

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 struct stu2
 6 {
 7     //成员列表
 8     //char name[21];
 9     char * name;
10     float * scores;
11 };
12 
13 int main()
14 {
15     struct stu2 *p = (struct stu2 *)malloc(sizeof(struct stu2) * 3);
16     for (int i = 0; i < 3; i++)
17     {
18         p[i].name = (char *)malloc(sizeof(char) * 21);
19         p[i].scores = (float *)malloc(sizeof(float) * 3);
20 
21         printf("请您输入学生 姓名   成绩 :\n");
22         scanf("%s%f%f%f", p[i].name, &p[i].scores[0], &p[i].scores[1], &p[i].scores[2]);
23 
24     }
25 
26     //冒泡排序
27     for (int i = 0; i < 3 - 1; i++)
28     {
29         for (int j = 0; j < 3 - i - 1; j++)
30         {
31             float sum1 = p[j].scores[0] + p[j].scores[1] + p[j].scores[2];
32             float sum2 = p[j + 1].scores[0] + p[j + 1].scores[1] + p[j + 1].scores[2];
33             if (sum1 > sum2)
34             {
35                 struct stu2 temp = p[j];
36                 p[j] = p[j + 1];
37                 p[j + 1] = temp;
38             }
39         }
40     }
41 
42 
43     for (int i = 0; i < 3; i++)
44     {
45 
46         printf("姓名:%s\n", p[i].name);
47         printf("成绩: %.1f   %.1f   %.1f\n", p[i].scores[0], p[i].scores[1], p[i].scores[2]);
48     }
49 
50 
51     //释放
52     for (int i = 0; i < 3; i++)
53     {
54         free(p[i].name);
55         free(p[i].scores);
56     }
57 
58     free(p);
59 
60 
61 
62     system("pause");
63     return EXIT_SUCCESS;
64 }

结构体和函数

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 struct info
 7 {
 8     char name[21];
 9     int age;
10 };
11 
12 void fun01(struct info s)
13 {
14     strcpy(s.name, "李四");
15     s.age = 20;
16 
17     printf("%s   %d\n", s.name, s.age);
18 
19 }
20 void fun02(struct info *s)
21 {
22     strcpy(s->name, "李四");
23     s->age = 20;
24 
25 }
26 
27 
28 
29 int main1()
30 {
31     struct info s = { "张三",18 };
32 
33     //fun01(s);
34     fun02(&s);
35 
36     printf("%s   %d\n", s.name, s.age);
37 
38     system("pause");
39     return EXIT_SUCCESS;
40 }
41 struct info  fun03()
42 {
43     struct info s;
44     strcpy(s.name, "李四");
45     s.age = 20;
46 
47     return s;
48 
49 }
50 struct info * fun04()
51 {
52     struct info s;
53     strcpy(s.name, "李四");
54     s.age = 20;
55 
56     return &s;
57 }
58 
59 //int fun04()
60 //{
61 //    int a = 10;
62 //    return a;
63 //}
64 int main()
65 {
66     struct info s = fun03();
67     printf("%s   %d\n", s.name, s.age);
68 
69 
70     //int a = fun04();
71 
72 
73     //struct info * s = fun04();
74     //printf("%s   %d\n", s->name, s->age);
75     system("pause");
76     return 0;
77 
78 }

 结构体嵌套结构体

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 struct stra
 7 {
 8     int a;  //4   4
 9     float b;//4   8
10     char c;//1   12  9
11     char arr[7];   //16
12     double h;//24
13 }abc;
14 
15 struct  strb
16 {
17     struct stra abc;//12  16
18     short f;  //2
19     char * e;  //4
20     short g;
21     //double d; //8
22 };
23 
24 
25 /*
26     技能cd:
27     skill01 10
28     skill02 3
29     skill03 7
30 
31 */
32 /*
33 
34     名称
35     等级
36     攻击
37     技能:
38 
39 
40 */
41 int main()
42 {
43     struct strb  strbb;
44     //strbb.d = 10.0f;
45     //strbb.abc.a = 100;
46 
47 
48     //printf("%d\n", strbb.abc.a);
49 
50     printf("%d\n", sizeof(strbb));
51 
52 
53     system("pause");
54     return EXIT_SUCCESS;
55 }

共用体

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 //共用体   union 共用体名称  成员列表  共用体变量名
 7 union vars
 8 {
 9     double a;
10     float b;
11     int c;
12     short d;
13     char f;
14     //char arr[17];//17
15 }var;
16 int main()
17 {
18     printf("%d\n", sizeof(var));
19     var.a = 100;
20     var.b = 3.14;
21     var.c = 66;
22     printf("%f\n", var.a);
23     printf("%f\n", var.b);
24     printf("%d\n", var.c);
25 
26     system("pause");
27     return EXIT_SUCCESS;
28 }

技术分享图片

 

C语言结构体训练

标签:分享图片   经验   warning   img   long   class   fun   war   clu   

原文地址:https://www.cnblogs.com/WangGuiHandsome/p/9899005.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!