Question: #define _GNU_SOURCE // for asprintf(), if needed #include // for printf() (and asprintf(), if needed) #include // for malloc() and free() #include // for strlen()
#define _GNU_SOURCE // for asprintf(), if needed #include// for printf() (and asprintf(), if needed) #include // for malloc() and free() #include // for strlen() and strcat(), if needed #include // just in case char *catStrings(char *strs[], int nStrs) { // Modify the body of this function as requested. Do not modify // any other part of this file. } int main(int argc, char *argv[]) { char *result = catStrings(argv+1, argc-1); printf("%s ", result); free(result); return 0; }
Using the above code in C, how would I:
- Find out how many bytes need to be malloc()'d. (Don't forget to allow for the spaces and the null terminator.)
- malloc() the result.
- Copy each member of strs to the result, followed by a space, except for the last member. (Equivalently, preceed each member by a space, except for the first one.)
- Return the result.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
