Question: In the header file for this question, a struct called Superhero is defined like this: typedef struct { char * name; short feetInHeight; short inchesInHeight;

In the header file for this question, a struct called Superhero is defined like this: typedef struct {
char* name;
short feetInHeight;
short inchesInHeight;
char* superpower;
char* traits;
} Superhero;
First, write a function that takes in 5 parameters: a pointer to a char array representing a name, two shorts representing feet in height and inches in height respectively, a pointer to a char array representing a superpower, and a pointer to a char array representing the traits; and returns the address of a dynamically (i.e., uses malloc) created Superhero struct variable storing those parameters.
Use this function header:
Superhero* createSuperhero(const char* name, short feetInHeight, short inchesInHeight, const char* superpower, const char* traits)
For example, given the code (name, superpower and traits are Cstrings storing the proper information):
Superhero* superhero = createSuperhero(name,6,0, superpower, traits);
printf("%s
%d\'%d\"
%s: %s
", superhero->name, superhero->feetInHeight, superhero->inchesInHeight, superhero->superpower, superhero->traits);
will result in an output like this:
Thunderstrike 6'0" Weather Control: Harnessing storms, Thunderstrike commands thunder and lightning, using nature's fury to protect the innocent and fight against evil.
You can assume all the height measurements are valid (i.e., feet & inches will not be negative), and all the Cstrings are properly formed (\0 terminated). Field variables name, superpower, and traits in the struct must be created dynamically and are copies of the parameters, instead of simply pointing to the parameters addresses. This is called deep-copy.
Next, write another function that takes in 1 parameter: the address of a Superhero struct variable; and releases (i.e., uses free) the memory created for the 3 field variables name, superpower, and traits.
Use this function header:
void clearSuperhero(Superhero* superhero)
Note that the parameter can be NULL (if so the function should do nothing). Also, this function does not release the memory used for the struct variable, but only those used by the variables fields. To release all the memory dynamically allocated for the struct variable, you should call the free() function with the address of this struct variable right after the function returns. For details read Question 3.
Only include the a2_question2.h header file and the function definitions (and your helper functions, if any) in the source file and name it as a2_question2.c. Do not use recursion in your answer.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!