Question: Program: malloc, free, pointer, function in C main char str[] = this is a test only just a test; /* or other string */ char

Program: malloc, free, pointer, function in C

main

char str[] = this is a test only just a test; /* or other string */

char *ptr1[100], *ptr2[100];

call function1; call function2;

Function1: tokenize the string strwith a pointer, and record each token (NULL terminated) in ptr1 array (i.e., skip white spaces in str)

ptr1[0] = this; ptr1[1] = is; ptr1[2] = a, etc

Function2:

Loop: print ptr1[i] that points to valid word

Loop: for each ptr2[i], malloc and copy the string from ptr1[i], print ptr2[i]

Error handling: copy string only if malloc is successful

Loop: print the pointer value (starting addrof token) of ptr1[i] and ptr2[i]ptr1[i] and ptr2[i] should have different values

Loop: free each of ptr2[i]

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!