- #include <stdio.h>
- #include <iostream>
- #include <malloc.h>
- /*
- Author: Wu Yongcong
- Program: structure pointer, structure variable nesting, structure pointer nesting, function pointer, array pointer, pointer array, typedef comprehensive application
- date:2017.6.3
- sum up: struct nested pointers (function pointers, struct pointers, array pointers, etc.) need to point to an appropriate address for this nested pointer, which should be a nested pointer address
- Key words note distinguish two different points of P3 the first P3 is just a member of a struct "variable name" The second P3 represents a struct "address value"
- */
- typedef unsigned int OS;
- typedef struct MANAGER //third-level struct
- {
- OS age;
- OS sex;
- void (*manage) ();
- }manager,*pmanage;
- typedef struct TEACHER //second layer structure
- {
- OS age;
- OS sex;
- void (*teach) ();
- Manager Manager;
- Pmanage P3; //nested struct pointers are initialized before using P3
- }teacher,*pteacher;
- typedef struct student{ //First layer structure
- OS age;
- OS sex;
- Char *name;
- OS (*array) [5]; //Array pointers
- OS *array1[10]; //array of pointers
- void (*learn) (); //function pointer
- Teacher Teacher; //nested struct general type
- Pteacher P2; //nested struct pointers are initialized before using P2
- }student,*pstu;
- void Manage ()
- {
- printf ("I am a manager\n");
- }
- void Teach ()
- {
- printf ("I am a teacher\n");
- }
- void Learn ()
- {
- printf ("I am a student\n");
- }
- Os array[5]={0};
- int main (_in_ int _argc, _in_reads_ (_ARGC) _pre_z_ char * * _ARGV, _in_z_ char * * _env)
- {
- Student Student;
- (1) typedef defines a generic type name
- Pstu p1= (pstu) malloc (sizeof (Student));
- Pteacher p2= (pteacher) malloc (sizeof (Teacher));
- Pmanage p3= (pmanage) malloc (sizeof (Manager));
- Char *name1= "WYC";
- Char a[]={' W ', ' Y ', ' C '};
- OS c=3;
- Teacher T;
- T.teach=teach;
- T.teach ();
- (2) typedef defines struct pointers, which need to be initialized before using the struct body pointer
- p1->age=3;
- p1->sex=4;
- p1->learn=learn; function pointers
- P1->learn ();//function pointer
- p1->name=name1;
- p1->name=a;
- (3) Double nesting of common structure variables nested inside structure body
- P1->teacher. Teach=teach;
- P1->teacher. Teach ();
- (*P1). Learn (); //In the form of *p represents a large memory operation equivalent to Student Stu
- (4) Structure pointer nested structure body pointer double nested
- p1->p2=p2;//the key word
- p1->p2->teach=teach;
- P1->p2->teach ();
- (*P1) .p2->teach=teach;
- (* ((*P1). P2)). Teach ();
- (5) Structure pointer nested structure pointer nested structure general variable triple nesting
- ((p1->teacher). Manager). Manage=manage;
- P1->teacher.manager.manage ();
- (6) Structure pointer nested structure pointer nested structure pointer triple nesting
- p1->p2->p3=p3; //Key words note distinguish between two different points of P3 the first P3 is just a member of a struct "variable name" The second P3 represents a struct "address value"
- p1->p2->p3->manage=manage;
- P1->p2->p3->manage ();
- (7) struct Access array pointer
- p1->array=&array;
- (8) struct Access pointer array
- p1->array1[0]=&c;
- p1->array1[1]=&c;
- p1->array1[2]=&c;
- p1->array1[3]=&c;
- Free (p1);
- Free (p2);
- Free (p3);
- System ("pause");
- return 0;
- }
(60) Structure pointer, structure variable nesting, structure pointer nesting, function pointer, array pointer, pointer array, typedef comprehensive application