SlideShare a Scribd company logo
1 of 2
Download to read offline
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
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.

More Related Content

Similar to Can anyone help me with 21 23 25 27 In C programming.pdf

CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndEdward Chen
 
Javascript Arrow function
Javascript Arrow functionJavascript Arrow function
Javascript Arrow functiontanerochris
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programmingTaseerRao
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)Arpit Meena
 
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdfDeclaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdfmalavshah9013
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingAMAN ANAND
 
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONSINTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONSKalaivaniD12
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdfNehaSpillai1
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxpriestmanmable
 
The Awesome Python Class Part-3
The Awesome Python Class Part-3The Awesome Python Class Part-3
The Awesome Python Class Part-3Binay Kumar Ray
 
Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptxrohithprabhas1
 
Hub102 - JS - Lesson3
Hub102 - JS - Lesson3Hub102 - JS - Lesson3
Hub102 - JS - Lesson3Tiểu Hổ
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes NUST Stuff
 

Similar to Can anyone help me with 21 23 25 27 In C programming.pdf (20)

functionnotes.pdf
functionnotes.pdffunctionnotes.pdf
functionnotes.pdf
 
CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2nd
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
 
Javascript Arrow function
Javascript Arrow functionJavascript Arrow function
Javascript Arrow function
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 
Function in C++
Function in C++Function in C++
Function in C++
 
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdfDeclaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONSINTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
 
memory
memorymemory
memory
 
Link list
Link listLink list
Link list
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
 
The Awesome Python Class Part-3
The Awesome Python Class Part-3The Awesome Python Class Part-3
The Awesome Python Class Part-3
 
Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptx
 
Hub102 - JS - Lesson3
Hub102 - JS - Lesson3Hub102 - JS - Lesson3
Hub102 - JS - Lesson3
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes
 

More from akshpatil4

Calculate the amount of money Paige had to deposit in an inv.pdf
Calculate the amount of money Paige had to deposit in an inv.pdfCalculate the amount of money Paige had to deposit in an inv.pdf
Calculate the amount of money Paige had to deposit in an inv.pdfakshpatil4
 
Cada ao en los Estados Unidos cientos de iglesias cristian.pdf
Cada ao en los Estados Unidos cientos de iglesias cristian.pdfCada ao en los Estados Unidos cientos de iglesias cristian.pdf
Cada ao en los Estados Unidos cientos de iglesias cristian.pdfakshpatil4
 
Can you please add comments so I can understand it Thank yo.pdf
Can you please add comments so I can understand it Thank yo.pdfCan you please add comments so I can understand it Thank yo.pdf
Can you please add comments so I can understand it Thank yo.pdfakshpatil4
 
can you help me write this code in Net Maui App please than.pdf
can you help me write this code in Net Maui App please than.pdfcan you help me write this code in Net Maui App please than.pdf
can you help me write this code in Net Maui App please than.pdfakshpatil4
 
Can you help me with these two questions I will give you a .pdf
Can you help me with these two questions I will give you a .pdfCan you help me with these two questions I will give you a .pdf
Can you help me with these two questions I will give you a .pdfakshpatil4
 
Can you help me design this interface in HTML Flight Find.pdf
Can you help me design this interface in HTML   Flight Find.pdfCan you help me design this interface in HTML   Flight Find.pdf
Can you help me design this interface in HTML Flight Find.pdfakshpatil4
 
Can You build the web based project Homepage for me Home .pdf
Can You build the web based project Homepage for me   Home .pdfCan You build the web based project Homepage for me   Home .pdf
Can You build the web based project Homepage for me Home .pdfakshpatil4
 
Can You build the web based project for me Home Page Aft.pdf
Can You build the web based project for me   Home Page Aft.pdfCan You build the web based project for me   Home Page Aft.pdf
Can You build the web based project for me Home Page Aft.pdfakshpatil4
 
can you break this down in python codes please Task 1 Cre.pdf
can you break this down in python codes please  Task 1 Cre.pdfcan you break this down in python codes please  Task 1 Cre.pdf
can you break this down in python codes please Task 1 Cre.pdfakshpatil4
 
Can someone explain this to me step by step Consider a give.pdf
Can someone explain this to me step by step Consider a give.pdfCan someone explain this to me step by step Consider a give.pdf
Can someone explain this to me step by step Consider a give.pdfakshpatil4
 
Can you answer these True or False questions for me please .pdf
Can you answer these True or False questions for me please .pdfCan you answer these True or False questions for me please .pdf
Can you answer these True or False questions for me please .pdfakshpatil4
 
Can someone please help with this Now that you have compl.pdf
Can someone please help with this  Now that you have compl.pdfCan someone please help with this  Now that you have compl.pdf
Can someone please help with this Now that you have compl.pdfakshpatil4
 
Can someone solve the TODO parts of the following problem i.pdf
Can someone solve the TODO parts of the following problem i.pdfCan someone solve the TODO parts of the following problem i.pdf
Can someone solve the TODO parts of the following problem i.pdfakshpatil4
 
Calculate the amount of money Dylan had to deposit in an inv.pdf
Calculate the amount of money Dylan had to deposit in an inv.pdfCalculate the amount of money Dylan had to deposit in an inv.pdf
Calculate the amount of money Dylan had to deposit in an inv.pdfakshpatil4
 
Can someone please help me implement the addSong function .pdf
Can someone please help me implement the addSong function .pdfCan someone please help me implement the addSong function .pdf
Can someone please help me implement the addSong function .pdfakshpatil4
 
Can someone please help me complete the add_song function .pdf
Can someone please help me complete the add_song function .pdfCan someone please help me complete the add_song function .pdf
Can someone please help me complete the add_song function .pdfakshpatil4
 
can someone please assist me with analysis of this case atud.pdf
can someone please assist me with analysis of this case atud.pdfcan someone please assist me with analysis of this case atud.pdf
can someone please assist me with analysis of this case atud.pdfakshpatil4
 
can someone help me with this class ENVS 200040 Pollution.pdf
can someone help me with this class ENVS 200040   Pollution.pdfcan someone help me with this class ENVS 200040   Pollution.pdf
can someone help me with this class ENVS 200040 Pollution.pdfakshpatil4
 
Can someone help me with these questions 1 Main structures.pdf
Can someone help me with these questions 1 Main structures.pdfCan someone help me with these questions 1 Main structures.pdf
Can someone help me with these questions 1 Main structures.pdfakshpatil4
 
Can someone help with these functions doAddTweet doEditTwe.pdf
Can someone help with these functions  doAddTweet doEditTwe.pdfCan someone help with these functions  doAddTweet doEditTwe.pdf
Can someone help with these functions doAddTweet doEditTwe.pdfakshpatil4
 

More from akshpatil4 (20)

Calculate the amount of money Paige had to deposit in an inv.pdf
Calculate the amount of money Paige had to deposit in an inv.pdfCalculate the amount of money Paige had to deposit in an inv.pdf
Calculate the amount of money Paige had to deposit in an inv.pdf
 
Cada ao en los Estados Unidos cientos de iglesias cristian.pdf
Cada ao en los Estados Unidos cientos de iglesias cristian.pdfCada ao en los Estados Unidos cientos de iglesias cristian.pdf
Cada ao en los Estados Unidos cientos de iglesias cristian.pdf
 
Can you please add comments so I can understand it Thank yo.pdf
Can you please add comments so I can understand it Thank yo.pdfCan you please add comments so I can understand it Thank yo.pdf
Can you please add comments so I can understand it Thank yo.pdf
 
can you help me write this code in Net Maui App please than.pdf
can you help me write this code in Net Maui App please than.pdfcan you help me write this code in Net Maui App please than.pdf
can you help me write this code in Net Maui App please than.pdf
 
Can you help me with these two questions I will give you a .pdf
Can you help me with these two questions I will give you a .pdfCan you help me with these two questions I will give you a .pdf
Can you help me with these two questions I will give you a .pdf
 
Can you help me design this interface in HTML Flight Find.pdf
Can you help me design this interface in HTML   Flight Find.pdfCan you help me design this interface in HTML   Flight Find.pdf
Can you help me design this interface in HTML Flight Find.pdf
 
Can You build the web based project Homepage for me Home .pdf
Can You build the web based project Homepage for me   Home .pdfCan You build the web based project Homepage for me   Home .pdf
Can You build the web based project Homepage for me Home .pdf
 
Can You build the web based project for me Home Page Aft.pdf
Can You build the web based project for me   Home Page Aft.pdfCan You build the web based project for me   Home Page Aft.pdf
Can You build the web based project for me Home Page Aft.pdf
 
can you break this down in python codes please Task 1 Cre.pdf
can you break this down in python codes please  Task 1 Cre.pdfcan you break this down in python codes please  Task 1 Cre.pdf
can you break this down in python codes please Task 1 Cre.pdf
 
Can someone explain this to me step by step Consider a give.pdf
Can someone explain this to me step by step Consider a give.pdfCan someone explain this to me step by step Consider a give.pdf
Can someone explain this to me step by step Consider a give.pdf
 
Can you answer these True or False questions for me please .pdf
Can you answer these True or False questions for me please .pdfCan you answer these True or False questions for me please .pdf
Can you answer these True or False questions for me please .pdf
 
Can someone please help with this Now that you have compl.pdf
Can someone please help with this  Now that you have compl.pdfCan someone please help with this  Now that you have compl.pdf
Can someone please help with this Now that you have compl.pdf
 
Can someone solve the TODO parts of the following problem i.pdf
Can someone solve the TODO parts of the following problem i.pdfCan someone solve the TODO parts of the following problem i.pdf
Can someone solve the TODO parts of the following problem i.pdf
 
Calculate the amount of money Dylan had to deposit in an inv.pdf
Calculate the amount of money Dylan had to deposit in an inv.pdfCalculate the amount of money Dylan had to deposit in an inv.pdf
Calculate the amount of money Dylan had to deposit in an inv.pdf
 
Can someone please help me implement the addSong function .pdf
Can someone please help me implement the addSong function .pdfCan someone please help me implement the addSong function .pdf
Can someone please help me implement the addSong function .pdf
 
Can someone please help me complete the add_song function .pdf
Can someone please help me complete the add_song function .pdfCan someone please help me complete the add_song function .pdf
Can someone please help me complete the add_song function .pdf
 
can someone please assist me with analysis of this case atud.pdf
can someone please assist me with analysis of this case atud.pdfcan someone please assist me with analysis of this case atud.pdf
can someone please assist me with analysis of this case atud.pdf
 
can someone help me with this class ENVS 200040 Pollution.pdf
can someone help me with this class ENVS 200040   Pollution.pdfcan someone help me with this class ENVS 200040   Pollution.pdf
can someone help me with this class ENVS 200040 Pollution.pdf
 
Can someone help me with these questions 1 Main structures.pdf
Can someone help me with these questions 1 Main structures.pdfCan someone help me with these questions 1 Main structures.pdf
Can someone help me with these questions 1 Main structures.pdf
 
Can someone help with these functions doAddTweet doEditTwe.pdf
Can someone help with these functions  doAddTweet doEditTwe.pdfCan someone help with these functions  doAddTweet doEditTwe.pdf
Can someone help with these functions doAddTweet doEditTwe.pdf
 

Recently uploaded

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 

Recently uploaded (20)

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 

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.