Question: Coding problem to solve: Delete a dynamic array of structures Objective: Write a function ` delete _ present _ list ` to free all memory

Coding problem to solve: Delete a dynamic array of structures
Objective: Write a function `delete_present_list` to free all memory allocated for an array of structures containing dynamically allocated strings and return a null pointer in c
Function Signature to use for the program:
struct present* delete_present_list(struct present* present_list, int num);
Structure Definition:
struct present {
char *present_name;
float price;
}
Edge Cases, must account for all:
Null Pointer for present_list: When passed to the function, the present_list is NULL. The function should gracefully handle this case by returning NULL immediately.
Zero Presents (num =0): The num parameter is 0, indicating no presence in the list. The function should handle this case without attempting to access or free any memory.
Null Pointers within present_list: Some present_name pointers within the present_list array are NULL. The function should check each present_name before attempting to free it.
Memory Allocation Failures (Hypothetical): consider what will happen if present_name allocations had failed during the list's creation. This might require handling NULL pointers within the array.
Criteria for code, must include all:
Correct function signature and parameter types.
Correctly handles the case where present_list is NULL
Properly iterates through the array to access each structure
Correctly frees each dynamically allocated string in the array
Correctly frees the dynamically allocated array
Returns `NULL` after freeing all allocated memory.
Clear, well-commented, and properly formatted code
Handles edge cases, such as an empty array or `NULL``present_name` pointers within the array.

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 Databases Questions!