(60) Structure pointer, structure variable nesting, structure pointer nesting, function pointer, array pointer, pointer array, typedef comprehensive application

Source: Internet
Author: User

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <malloc.h>
  4. /*
  5. Author: Wu Yongcong
  6. Program: structure pointer, structure variable nesting, structure pointer nesting, function pointer, array pointer, pointer array, typedef comprehensive application
  7. date:2017.6.3
  8. 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
  9. 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"
  10. */
  11. typedef unsigned int OS;
  12. typedef struct MANAGER //third-level struct
  13. {
  14. OS age;
  15. OS sex;
  16. void (*manage) ();
  17. }manager,*pmanage;
  18. typedef struct TEACHER //second layer structure
  19. {
  20. OS age;
  21. OS sex;
  22. void (*teach) ();
  23. Manager Manager;
  24. Pmanage P3; //nested struct pointers are initialized before using P3
  25. }teacher,*pteacher;
  26. typedef struct student{ //First layer structure
  27. OS age;
  28. OS sex;
  29. Char *name;
  30. OS (*array) [5]; //Array pointers
  31. OS *array1[10]; //array of pointers
  32. void (*learn) (); //function pointer
  33. Teacher Teacher; //nested struct general type
  34. Pteacher P2; //nested struct pointers are initialized before using P2
  35. }student,*pstu;

  1. void Manage ()
  2. {
  3. printf ("I am a manager\n");
  4. }
  5. void Teach ()
  6. {
  7. printf ("I am a teacher\n");
  8. }
  9. void Learn ()
  10. {
  11. printf ("I am a student\n");
  12. }
  13. Os array[5]={0};
  14. int main (_in_ int _argc, _in_reads_ (_ARGC) _pre_z_ char * * _ARGV, _in_z_ char * * _env)
  15. {
  16. Student Student;
  17. (1) typedef defines a generic type name
  18. Pstu p1= (pstu) malloc (sizeof (Student));
  19. Pteacher p2= (pteacher) malloc (sizeof (Teacher));
  20. Pmanage p3= (pmanage) malloc (sizeof (Manager));
  21. Char *name1= "WYC";
  22. Char a[]={' W ', ' Y ', ' C '};
  23. OS c=3;
  24. Teacher T;
  25. T.teach=teach;
  26. T.teach ();
  27. (2) typedef defines struct pointers, which need to be initialized before using the struct body pointer
  28. p1->age=3;
  29. p1->sex=4;
  30. p1->learn=learn; function pointers
  31. P1->learn ();//function pointer
  32. p1->name=name1;
  33. p1->name=a;
  34. (3) Double nesting of common structure variables nested inside structure body
  35. P1->teacher. Teach=teach;
  36. P1->teacher. Teach ();
  37. (*P1). Learn (); //In the form of *p represents a large memory operation equivalent to Student Stu
  38. (4) Structure pointer nested structure body pointer double nested
  39. p1->p2=p2;//the key word
  40. p1->p2->teach=teach;
  41. P1->p2->teach ();
  42. (*P1) .p2->teach=teach;
  43. (* ((*P1). P2)). Teach ();
  44. (5) Structure pointer nested structure pointer nested structure general variable triple nesting
  45. ((p1->teacher). Manager). Manage=manage;
  46. P1->teacher.manager.manage ();
  47. (6) Structure pointer nested structure pointer nested structure pointer triple nesting
  48. 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"
  49. p1->p2->p3->manage=manage;
  50. P1->p2->p3->manage ();
  51. (7) struct Access array pointer
  52. p1->array=&array;
  53. (8) struct Access pointer array
  54. p1->array1[0]=&c;
  55. p1->array1[1]=&c;
  56. p1->array1[2]=&c;
  57. p1->array1[3]=&c;
  58. Free (p1);
  59. Free (p2);
  60. Free (p3);
  61. System ("pause");
  62. return 0;
  63. }

(60) Structure pointer, structure variable nesting, structure pointer nesting, function pointer, array pointer, pointer array, typedef comprehensive application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.