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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started