ProgrammingVery hard
char src[] = "hey"; char dst[8]; memcpy(dst, src, strlen(src)); printf("%d", (int)strlen(dst)); What is the problem with this code?
- A. It prints 3 with no problem
- B. memcpy does not copy the terminator, so strlen(dst) reads uninitialised bytes, which is undefined
- C. It prints 0 because memcpy clears dst
- D. It fails to compile because memcpy needs three arguments
Correct answer: B. memcpy does not copy the terminator, so strlen(dst) reads uninitialised bytes, which is undefined
The terminator is not copied, so dst is not a valid string. Use memcpy with strlen(src) + 1 bytes, or strcpy.
More C Programming questions
- int arr[3] = {1, 2, 3}; int (pa)[3] = &arr; printf("%d", (pa)[2]); What is printed?
- int spiralcount = 4 5; printf("%d", spiralcount); For a 4 x 5 matrix, how many cells does a full spiral visit?
- A program climbs a staircase of 5 steps, each move going up one or two steps. How many distinct ways are there?
- The number of subsets of a 4-element set with an even number of elements is what?
- The longest common subsequence of "abc" and "ac" has length?
- The sum of the entries of row 4 of Pascal's triangle (row 0 at the top) is what?