Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the following C code?

const char pla[] = "string1";
const char src[] = "string2";
printf("Before memmove place= %s, src = %s\n", pla, src);
memmove(pla, src, 7);
printf("After memmove place = %s, src = %s\n", pla, src);

 

  • A.Before memmove place= string1, src = string2 After memmove place = string2, src = string2
  • B.Before memmove place = string2, src = string2 After memmove place= string1, src = string2
  • C.Before memmove place = string2, src = string1 After memmove place= string2, src =string2
  • D.Before memmove place= string1, src = string2 After memmove place=string1, src = string1

Answer: A

Correct answer: Before memmove place= string1, src = string2 After memmove place = string2, src = string2

memmove copies 7 bytes from src to pla, that is the characters string2 without its terminating null - but pla already has a null in position 7, so it reads correctly as string2 afterwards. The source is not altered, which is why src is unchanged. (Note that both arrays are declared const, so writing to one is not something a program should do.)

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.