📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    Complete the C function to copy at most n characters

    from source starting at start_index into destination, ensuring destination is null-terminated. Assume destination has enough allocated memory. #include #include // For debugging, not strictly needed void safe_copy_substring(char* destination, const char* source, int start_index, int n) {     int source_len = strlen(source);     if (start_index >= source_len) {         destination[0] = '\0'; // Source index out of bounds, return empty string         return;     }     int chars_to_copy = n;     if (start_index + chars_to_copy > source_len) {         chars_to_copy = source_len - start_index;     }     strncpy(destination, _________, chars_to_copy); // Line to complete     destination[chars_to_copy] = '\0'; // Ensure null-termination }
    A source Correct Answer Incorrect Answer
    B source + start_index Correct Answer Incorrect Answer
    C &source[start_index] Correct Answer Incorrect Answer
    D source[start_index] Correct Answer Incorrect Answer
    E Both B and C are correct. Correct Answer Incorrect Answer

    Solution

    Correct Answer: E (Both source + start_index and &source[start_index] correctly provide a pointer to the starting character of the substring within source.)

    Practice Next
    ask-question