Question

A C function attempts to copy a substring. #include #include #include char* copy_substring(const char* source, int start_index, int length) {     char* dest = (char*)malloc(length + 1);     if (dest == NUL

  • L return NULL;     strncpy(dest, source + start_index, length);     dest[length] = '\0'; // Null-terminate     return dest; } int main() {     const char* original = "Programming"; // Length 11     char* sub = copy_substring(original, 5, 10); // start_index=5, length=10     if (sub) {         printf("Substring: %s ", sub);         free(sub);     }     return 0; } What is the most likely issue with copy_substring when called as in main?
A It will correctly copy "amming".
B It will copy "amming" but might read beyond the bounds of original, leading to undefined behavior.
C It will copy "amming" and then crash due to dest[length] = '\0' accessing invalid memory.
D It will cause a memory leak.
E It will return NULL because malloc fails.
Practice Next

Hey! Ask a query