#define _crt_secure_no_warnings#include <stdlib.h> #include <string.h> #include <stdio.h>// struct nested One-level pointer two-level pointer dynamically allocates memory typedef struct _TEACHER {int age;int id;char *title;char **pstuarray;char name[64];} teacher;//Print structure void printfteacher (Teacher *array,int num) {int i = 0;for (i = 0; i < num; i++) {printf ("%d\n", Array[i] . Age);p rintf ("%s\n", Array[i].pstuarray[i]);}} Allocates memory for struct teacher* createtarray (int num) {int i = 0,j=0; teacher* Teacher;teacher = (teacher*) malloc (sizeof (Teacher) *num), if (Teacher!=null) {for (i = 0; i < num; i++) {teacher[ I].title = (char*) malloc (sizeof (char) *100);//struct teacher title allocates memory char** ptmp = (char**) malloc (sizeof (char*) *num);// for (j = 0; j < Num; J + +) {Ptmp[i] = (char*) malloc;//}teacher[i].pstuarray = ptmp;}} return teacher;} Free memory void Freeteacher (teacher* arrayt,int num) {int i = 0,j=0;if (arrayt!=null) {for (i = 0; i < num;i++) {if (arrayt[i ].title! = NULL) {free (arrayt[i].title);} if (Arrayt[i].pstuarray!=null) {for (j = 0; j < num; j++) {free (arrayt[i].pstuarray[j]);}} Free (arrayt[i].pstuarray);} Free (Arrayt);}} void Main () {int i = 0,j=0; Teacher *parray=null;int num = 1;parray = Createtarray (num); for (i = 0; i < num; i++) {printf ("%d---age?\n", i + 1); SCA NF ("%d", &parray[i].age);p rintf ("%d---title?\n", i + 1), scanf ("%s", Parray[i].title);p rintf ("%d---name?\n", i + 1) ; scanf ("%s", Parray[i].name), for (j = 0; j < Num; J + +) {printf ("---student----%d\n", i+1), scanf ("%s", Parray[i]. PSTUARRAY[J]);}} Printfteacher (Parray,num); Freeteacher (Parray, num); system ("Pause");}
C struct nested One-level pointer two-level pointer dynamically allocating memory