1. 程式人生 > >C語言結構體訓練

C語言結構體訓練

結構體大小和記憶體結構

 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 }