Home / Expert Answers / Computer Science / c-programming-nbsp-1-given-the-structs-defined-below-fill-the-blanks-in-the-functions-to-complet-pa688

(Solved): C programming.  1. Given the structs defined below, fill the blanks in the functions to complet ...



C programming. 

1. Given the structs defined below, fill the blanks in the functions to complete the code.
(a) Step 1 Complete the function c
(b) Complete the function create. project to create a project instance on the heap.
2 Atruct project. ereate_project(char* pr
(d) Given the database (a pointer) as the input, complete the function print_db to print the project information with the fol
2. Given a pointer variable, we can use type casting to convert the pointer to a different type of pointer. For example, assu
1. Given the structs defined below, fill the blanks in the functions to complete the code. (a) Step 1 Complete the function create_people to create a people instance on the heap. The function takes two char pointers as inputs which point to two different strings, and it returns a struct people pointer. Note that the struct needs to be allocated on the heap, but the two pointers will point to strings on the data segment (as they are created with double quotes). 1 struct people* create_people (char* first_name, char* 1ast_name) \{ struct people* t_people; 1/ allocate space for struct on the heap t-people 11 set the pointers in the struct return t-people; \} (b) Complete the function create. project to create a project instance on the heap. 2 Atruct project. ereate_project(char* project_name, struct 4 struct project* t_project: 1/ allocate space for strict \( M \) get the pointers in the struct return t-project; 3 (c) In the main function, create a database as an array of project pointers also on the heap. The size of array should be 2 . The array name should be project_db. Read the rest of the code in main function to understand how this database will be used. int main() \{ \( 1 / \) Create an array of struct project pointers on the heap \( \ldots \) project_db \( = \) struct people* people_0 = create_people ("Daenerys", " Targaryen \( " \) ); struct people* people_1 = create_people("Jon", "Snow"); struct people* people_2 = create_people("Tony", "Stark"); project_db 0\( ] \) = create_project("Defeat Thanos", people_1, people_2); project_db d \( ] \) = create_project ("Defeat Night's King", people_0,people_1); return 0 ; 3 (d) Given the database (a pointer) as the input, complete the function print_db to print the project information with the following format. Assuming the database is populated with the code in the main functioin above, by calling the function with print db (project_db). it will print the following: Project: Defeat Thanos by Jon Snow and Tony Stark Project: Defeat Night's King by Daenerys Targaryen and Jon Snow 1 void print_db(struct project** db) f \( 1 / \) assume the size of the array is always 2 for (int \( i=0 ; i<2 ; i++)\{ \) printf( 3 (e) At the end of the main function, free all memory allocated on the heap. int main() \{ 1/ assume the code here follows after the line 12 in the main function before. /1 free each item individually, i.e., no loop. 3 2. Given a pointer variable, we can use type casting to convert the pointer to a different type of pointer. For example, assuming \( x p \) is an integer pointer (i.e., int-), you can use the code below to conver it to a character pointer. \begin{tabular}{l|l} 1 & 1nt \( x=0 \times 41424344: \) \\ 2 & int \( x p=2 x ; \) \\ 3 & chax cp \( =( \) chax \( *) \) xp: \\ 4 & for \( ( \) int i \( =0 ; 1<4 ; 1+4) \) \\ 5 & printif \( \left.* \% c^{\prime \prime}, \operatorname{cp}[1]\right) ; \) \end{tabular} In this case, since the size of int is four bytes, we can use cp to access this four-bytes number as if it is an army of four characters. Given this, what do think the code above will print out? (Notes The computer here uses Little Endian.) 3. Greate 2D array on the heap with the minimal malloc calling. The return pointer must be the type of int \( { }^{*} * \); so that we can use retrieve any entry by using array \( [1][j] \). HINT: The best solution only calls malloc once and nceds to use pointer type casting.


We have an Answer from Expert

View Expert Answer

Expert Answer


a) We allocate space using malloc keyword as shown below t_people = malloc(sizeof(people)); t_people->first_name = first_name; t_people->last_name = l
We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe