Coding Challenge: Modeling Complex Objects and Relationships (2)
Explore how to model complex objects and their relationships using C structures. Learn to dynamically allocate and initialize memory for a flower shop business, manage continents and shops, handle memory cleanup, and implement sales and profit calculations to build efficient, pointer-based data management in C.
Task 6: Coding challenge
Now we have all the pieces needed to open a TFlowerShop, which we’ll add to the flower shops list using the function from Task 4. We’ll initialize the two flowers using the function from Task 5.
Write a function called openFlowerShop, which will allocate, initialize, and return a pointer to a TFlowerShop structure.
The function header is:
TFlowerShop* openFlowerShop(TFlowerBusiness* flowerBusiness, int continentId, char* name, char* address, int openingYear)
flowerBusinessis the current business.continentIdis the ID of the continent where we want to open the business.name,address, andopeningYearare used to initialize some of the members inside theTFLowerShop.- The function must return
NULLon failure. - It must also make sure to allocate and initialize all the members correctly.
- After we allocate the flower shop, we must add it to the flower shops list of that continent. You may want to use the function from Task 4, but check its return value and guard against
reallocfails. - What happens if this shop is the first shop in the continent? In other words, this is the first time our business opens a shop in the new continent. You may want to use the function
allocContinentfrom Task 3.
This task ...