Question: Question 2 [ 4 marks ] In the header file for this question, a struct called Superhero is defined like this: First, write a function
Question marks
In the header file for this question, a struct called Superhero is defined like this:
First, write a function that takes in 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
ie uses malloc created Superhero struct variable storing those parameters.
Use this function header:
Superhero createSuperheroconst 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 createSuperheroname superpower, traits;
printfs
dd
s: s
superheroname, superherofeetInHeight,
superheroinchesInHeight, superherosuperpower, superherotraits;
will result in an output like this:
Thunderstrike
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 ie feet & inches will not be negative and all the
Cstrings are properly formed 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 "deepcopy".
Next, write another function that takes in parameter: the address of a Superhero struct variable; and
releases ie uses free the memory created for the field variables name, superpower, and traits.
Use this function header:
void clearSuperheroSuperhero 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 variable's 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
Only include the aquestion h header file and the function definitions and your helper functions, if any
in the source file and name it as aquestionc Do not use recursion in your answer.
please code this in C using only stdlib.h string.h and stdio.h libraries, in the context of a first year coding course :)
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
