Question 6
Back to Exercise
What is the output of the following C program on a 64-bit system?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
const char *src = "heap";
char *copy = malloc(strlen(src) + 1);
if (copy == NULL)
return 1;
strcpy(copy, src);
copy[0] = 'H';
printf("%s %s %zu\n", src, copy, strlen(copy) + 1);
free(copy);
return 0;
}
- A heap Heap 5 Correct
- B Heap Heap 5
- C heap Heap 4
- D heap heap 5
Correct Answer: Option A
The copy is a separate, writable block, so changing it leaves the literal alone. It needs strlen + 1 = 5 bytes for the terminator.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!