zl程序教程

您现在的位置是:首页 >  其他

当前栏目

35深入理解C指针之---结构体基础

基础 深入 理解 结构 --- 指针 35
2023-09-11 14:22:23 时间

  一、结构体基础

    1、定义:结构体大大加强了C的数据聚合能力,可以使得不同类型的数据进行结合

    2、特征:

      1)、结构体可以使得不同类型的数据进行结合

      2)、结构体可以使用内置的数据类型,包括指针

      3)、结构体可以使用自定义的数据类型,甚至自身(必须是命名结构体)

      4)、结构体成员的访问通过成员访问符号'.',若是结构体指针时,访问成员变量时可以使用->

      5)、结构体的普通定义

      6)、结构体的类型定义

      7)、结构体指针的定义及应用

      8)、结构体的大小

      9)、结构体其他
      

    3、基本定义及应用:

 1 #include <stdio.h>
 2 #include <string.h>
 3
 4 int main(int argc, char **argv)
 5 {
 6     struct _person{
 7         char firstName[10];
 8         char lastName[30];
 9         char title[100];
10         int age;
11     };
12
13     struct _person person1;
14     strcpy(person1.firstName, "guo");
15     strcpy(person1.lastName, "zhumulama");
16     strcpy(person1.title, "study hard every day!");
17     person1.age = 39;
18
19     printf("usual struct define:\n");
20     printf("The person1 info:\n firstName: %s\n lastName: %s\n title: %s\n and age: %d\n", person1.firstName, person1.lastName, person1.title, p   erson1.age);
21
22     typedef struct person{
23         char firstName[10];
24         char lastName[30];
25         char title[100];
26         int age;
27     } Person;
28
29     Person person2;
30     strcpy(person2.firstName, "zhang");
31     strcpy(person2.lastName, "hangchao");
32     strcpy(person2.title, "study hard every year!");
33     person2.age = 42;
34
35     printf("typedef struct define:\n");
36     printf("The person2 info:\n firstName: %s\n lastName: %s\n title: %s\n and age: %d\n", person2.firstName, person2.lastName, person2.title, p   erson2.age);
37
38     return 0;
39 }

    代码说明:

      1)、第6-11行代码是结构体的普通定义

      2)、若使用普通定义,声明结构体的方式如13行所示

      3)、第22-27行代码是结构体的类型定义
      
      4)、若使用类型定义,声明结构体的方式如29行所示

      5)、第20行和36行,演示了结构体成员的访问;

      6)、一般情况下,使用结构体的类型定义比较方便;


    4、结构体指针及结构体成员指针的应用:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4
 5 int main(int argc, char **argv)
 6 {
 7     typedef struct _person{
 8         char *firstName;
 9         char *lastName;
10         char *title;
11         int age;
12     } Person;
13
14     Person person1;
15     person1.firstName = (char *)malloc(strlen("zhang") + 1);
16     strcpy(person1.firstName, "zhang");
17     person1.lastName = (char *)malloc(strlen("hangchao") + 1);
18     strcpy(person1.lastName, "hangchao");
19     person1.title= (char *)malloc(strlen("study hard every year!") + 1);
20     strcpy(person1.title, "study hard every year!");
21     person1.age = 42;
22
23     printf("The person1 info:\n firstName: %s\n lastName: %s\n title: %s\n and age: %d\n", person1.firstName, person1.lastName, person1.title, person1.age);
24     printf("\n");
25
26     Person *ptrPerson = (Person *)malloc(sizeof(Person));
27     (*ptrPerson).firstName = (char *)malloc(strlen("wangu") + 1);
28     strcpy((*ptrPerson).firstName, "wangu");
29     (*ptrPerson).lastName = (char *)malloc(strlen("chaohang") + 1);
30     strcpy((*ptrPerson).lastName, "chaohang");
31     (*ptrPerson).title= (char *)malloc(strlen("study hard every dayd!") + 1);
32     strcpy((*ptrPerson).title, "study hard every dayd!");
33     (*ptrPerson).age = 42;
34
35     printf("The (*ptrPerson). info:\n firstName: %s\n lastName: %s\n title: %s\n and age: %d\n", (*ptrPerson).firstName, (*ptrPerson).lastName, (*ptrPerson).title, (*ptrPerson).age);
36     printf("\n");
37
38     Person *ptrPerson1 = (Person *)malloc(sizeof(Person));
39     ptrPerson1->firstName = (char *)malloc(strlen("wangu") + 1);
40     strcpy((*ptrPerson1).firstName, "wangu");
41     ptrPerson1->lastName = (char *)malloc(strlen("chaohang") + 1);
42     strcpy((*ptrPerson1).lastName, "chaohang");
43     ptrPerson1->title= (char *)malloc(strlen("study hard every dayd!") + 1);
44     strcpy((*ptrPerson1).title, "study hard every dayd!");
45     ptrPerson1->age = 42;
46
47     printf("The ptrPerson1-> info:\n firstName: %s\n lastName: %s\n title: %s\n and age: %d\n", ptrPerson1->firstName, ptrPerson1->lastName, ptrPerson1->title, ptrPerson1->age);
48     printf("\n");
49
50     return 0;
51 }

 

    代码说明:

      1)、第7-12行代码是结构体的类型定义,结构体成员为指针变量

      2)、第14行声明结构体变量

      3)、第15,17,19行代码是结构体变量成员分配内存
      
      4)、第16,18,20行代码是结构体变量成员赋值内容

      5)、第23行,演示了结构体成员的访问;

      6)、第26行和38行声明结构体指针变量;

      7)、第26行和38行为结构体指针变量申请内存;
      
      8)、第35行代码是结构体指针变量访问方式,使用(* structName).是结构体指针访问成员的老式方法,现在基本不用;

      9)、第47行代码是结构体指针变量访问方式,使用structName->是结构体指针访问成员的新式方法,现在用的比较频繁;