Can anyone help me with 2.1 , 2.3 ,2.5, 2.7 In C programming
2.1 Elephant *new_elephant(const char name[], enum type which_type, unsigned int id, unsigned
short weight, float trunk_length) Project #5 discussed some approaches for writing structure
functions in C. Another option not mentioned then is for a function to return a pointer to a structure
that the function has itself dynamically allocated. Using this approach a function can return a
pointer to an initialized structure without requiring the caller to declare a structure or to allocate
memory for : structure. It is common for functions to dynamically allocate a structure and return a
pointer to it after suitably initializing its fields. This function will use this approach. The function
should just return NULL without doing anything else if its array parameter is NULL. Otherwise it
should return a pointer to a dynamically-allocated Elephant structure that has the parameters'
values stored in its fields. The name field of the newly created Elephant structure has no fixed or
maximum length, so this function now must allocatea string of exactly the right size to store the
name parameter (however long it may be), copy the value of the name parameter to the allocated
string, and make the name field of the allocated structure point to it. To emphasize: the new
elephant's name field must point to new memory that this function allocates, not just point to the
parameter name that was passed in. In other words, this function should not just be doing pointer
aliasing. Use string library functions for all manipulations of the elephant's name, not loops (or
recursion), or you will lose considerable credit. 2.2 unsigned short get_type(const Elephant * const
el_ptr, enum type *const type_ptr) This function should "return" to the caller the type of its
parameter elephant, but it should also return 0 if either parameter is NULL. However, since the
first constant in an enum has the numeric value 0 , if the function only returned a value, the caller
would not be able to distinguish between it returning 0 to indicate that either parameter was NULL,
or it returning the enum value AFRICAN. What the function should do instead is to return 0 or 1
depending upon its parameters' validity, and, if the parameters are valid, to store the elephant's
type into the variable that type ptr points to. So if either parameter is NULL the function should
return 0 without changing anything else, while otherwise it should return 1 after storing its
parameter elephant's type into the variable that its second parameter points to. 2.3 const char
*get_name(const Elephant *const el_ptr) This function should return NULL if it parameter is NULL,
or if its parameter is not NULL but its parameter's name field is NULL. Otherwise it should return
its parameter elephant's name, but it should not just return a pointer to the name field of the
Elephant structure itself. Instead, it should allocate enough memory to store the name, copy th.
Can anyone help me with 21 23 25 27 In C programming.pdf
1. Can anyone help me with 2.1 , 2.3 ,2.5, 2.7 In C programming
2.1 Elephant *new_elephant(const char name[], enum type which_type, unsigned int id, unsigned
short weight, float trunk_length) Project #5 discussed some approaches for writing structure
functions in C. Another option not mentioned then is for a function to return a pointer to a structure
that the function has itself dynamically allocated. Using this approach a function can return a
pointer to an initialized structure without requiring the caller to declare a structure or to allocate
memory for : structure. It is common for functions to dynamically allocate a structure and return a
pointer to it after suitably initializing its fields. This function will use this approach. The function
should just return NULL without doing anything else if its array parameter is NULL. Otherwise it
should return a pointer to a dynamically-allocated Elephant structure that has the parameters'
values stored in its fields. The name field of the newly created Elephant structure has no fixed or
maximum length, so this function now must allocatea string of exactly the right size to store the
name parameter (however long it may be), copy the value of the name parameter to the allocated
string, and make the name field of the allocated structure point to it. To emphasize: the new
elephant's name field must point to new memory that this function allocates, not just point to the
parameter name that was passed in. In other words, this function should not just be doing pointer
aliasing. Use string library functions for all manipulations of the elephant's name, not loops (or
recursion), or you will lose considerable credit. 2.2 unsigned short get_type(const Elephant * const
el_ptr, enum type *const type_ptr) This function should "return" to the caller the type of its
parameter elephant, but it should also return 0 if either parameter is NULL. However, since the
first constant in an enum has the numeric value 0 , if the function only returned a value, the caller
would not be able to distinguish between it returning 0 to indicate that either parameter was NULL,
or it returning the enum value AFRICAN. What the function should do instead is to return 0 or 1
depending upon its parameters' validity, and, if the parameters are valid, to store the elephant's
type into the variable that type ptr points to. So if either parameter is NULL the function should
return 0 without changing anything else, while otherwise it should return 1 after storing its
parameter elephant's type into the variable that its second parameter points to. 2.3 const char
*get_name(const Elephant *const el_ptr) This function should return NULL if it parameter is NULL,
or if its parameter is not NULL but its parameter's name field is NULL. Otherwise it should return
its parameter elephant's name, but it should not just return a pointer to the name field of the
Elephant structure itself. Instead, it should allocate enough memory to store the name, copy the
parameter elephant's name to the newly-allocated memory, and return a pointer to this new copy
of the name. The caller will be responsible for freeing the returned memory later, if they want to
avoid memory leaks. (This is their responsibility to do, which your function cannot enforce.) Use
string library functions for all manipulations of the elephant's name, not loops (or recursion), or you
will lose considerable credit.2.4 void print_elephant(const Elephant const el_ptr) This function
should have no effect if its parameter is NULL, or if its parameter is not NULL but its parameter's
name field is NULL. Otherwise it should print its parameter elephant's fields, in the same manner
as print elephant() did in Project #5, except the elephant's name is to be printed first. (One of the
public tests illustrates the expected output format.) 2.5 short compare(const Elephant *const
el_ptr1, const Elephant *const el_ptr2) This function should return -1 if either of its parameters is
NULL, or if either parameter is not NULL but the elephant's name field is NULL. (Note that its
2. return type is now short, as opposed to unsigned short in the Project #5 version of this function.) If
its parameters are non-NULL it should return 1 if its two Elephant structure parameters have all
the same values, and 0 otherwise. Use string library functions for all manipulations of the
elephant's name, not loops (or recursion), or you will lose considerable credit. 2.6 unsigned short
change_name(Elephant *const el_ptr, const char new_name[]) This function should return 0
without changing anything if either parameter is NULL. Otherwise it should change the name field
of its elephant parameter to be new name and return 1 . (It is not a problem if the elephant's name
field is initially NULL, because the function is going to change the name anyway.) The function
must change the name by allocating a new string of exactly the right size to store the value of new
name, copy the contents of new name to the allocated string, and make the name field of the
structure that el pat r points to now point to the new allocated name. Note that el_ptr's name field
should be set to point to new memory that the function allocates, of exactly the right size needed,
and copy the contents of new name to the new memory. The function should not just set el pat r's
name field to point to new name itself. In other words, this function should not just be doing pointer
aliasing. But before reassigning the structure's name parameter the function should free the
memory of the elephant's current name, to avoid memory leaks.Use string library functions for all
manipulations of the elephant's name, not loops (or recursion), or you will lose considerable credit.
2.7 unsigned short copy(Elephant *const el_ptr1, const Elephant *const el_ptr2) This function
should return 0 without changing anything if either parameter is NULL, or if either parameter is not
NULL but the elephant's name field is NULL. Otherwise it should return 1 after copying all of the
fields of the structure that el_ptr2 points to into the structure that el_ptr1 points to. This function
should store new values into the fields of the El ephant structure that its first parameter points to.
However, as in new elephant( ) and change name ( ), this function should cause its first
parameter's name field to point to new memory that the function allocates that contains a copy of
el_ptr2's name, which is just the right size to store the name. el_ptr1's name should not just point
to el_ptr2's name field (the function should not just be doing pointer aliasing). But before making
el_ptrl's name field point to a new string, the function should free the memory used for el_ptrl's
current name, to avoid memory leaks. Important: the caller is passing in the memory addresses of
existing Elephant structures that were created before calling this function. This function has to
allocate memory for el_ptr1's new name, but not for the structure that el_pt r1 points to. The
Elephant structures that the parameters point to were created before this function is called. Use
string library functions for all manipulations of the elephant's name, not loops (or recursion), or you
wil lose considerable credit.