IN C
Consider the following function whose purpose is to return an array of 3 strings that are initialized to the strings in the character arrays s1, s2, and s3:
#include #include
char** stringstoarray(char* s1, char* s2, char* s3) {
// dynamically allocate space for the pointer array itself.
char** strings = fill in the blank
strings[ 0 ] = strdup(s1); // initialize first string
strings[ 1 ] = strdup(s2); // initialize first string
strings[ 2 ] = strdup(s3); // initialize third string
return strings;
}
In order for the function for the function to carry out its purpose, fill in the blank with the necessary code. Do not use spaces in your answer and don't forget a semi-colon at the end of your answer.
Answer is:
char** strings = (char**)malloc(sizeof(char*)*3+sizeof(char)*3*sizeof(s1));
Explanation: malloc function is used for dynamic allocation of space and here we are trying to allocate for 3 pointers of char* type and then also for each pointer which will be storing individual strings we are allocating space using the second term to the right of +, and we have to assume that all the strings are of same size and for that reason we have used sizeof(s1) otherwise we can use a value that is the maximum length upto which these strings can go.
Get Answers For Free
Most questions answered within 1 hours.