// C primer plus
// Use Pointer in Structure
// Allocate memory space to pointers in the Structure
// Free releases the previously allocated memory space
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include "tsong. H"
Struct namect {
Char * fname;
Char * lname;
Int letters;
};
Void getinfo (struct namect *); // enter the name
Void makeinfo (struct namect *); // calculates the length.
Void showinfo (const struct namect *); // print the information in the Structure
Void cleanup (struct namect *); // release memory
Int main (){
Struct namect person;
Getinfo (& person );
Makeinfo (& person );
Showinfo (& person );
Cleanup (& person );
Return 0;
}
Void getinfo (struct namect * PST)
{
Char temp [81];
Puts ("Please enter your first name ");
Gets (temp );
PST-> fname = (char *) malloc (strlen (temp) + 1 );
Strcpy (PSt-> fname, temp );
Puts ("Please enter your last name ");
Gets (temp );
PST-> lname = (char *) malloc (strlen (temp) + 1 );
Strcpy (PSt-> lname, temp );
}
Void makeinfo (struct namect * PST)
{
PST-> letters = strlen (PSt-> fname) + strlen (PSt-> lname );
}
Void showinfo (const struct namect * PST)
{
Printf ("% S % s, your name contains % d letters \ n", PSt-> fname, PSt-> lname, PSt-> letters );
}
Void cleanup (struct namect * PST)
{
Free (PSt-> fname );
Free (PSt-> lname );
}