What is wrong with the following code fragment? char str1[10] = "Mark", str2[15] = "What's my name"; strcpy \( (\operatorname{str} 1 \), str2); str1 does not have enough room. nothing str2 has white space in it. str2 does not have enough room.
Solución de tutoría real
Responder
Solución
¡Inicia sesión para desbloquear respuestas gratis!
Una plataforma de aprendizaje en la que confían millones de estudiantes y profesores reales.
Revisado y aprobado por el equipo de tutoría de UpStudy
Bonus Knowledge
The problem with the code fragment is that `str1` is declared with a size of 10 characters, but the `strcpy` function is trying to copy the entire `str2`, which contains the string "What's my name" (16 characters including the null terminator). This results in a buffer overflow, which can lead to unpredictable behavior or even program crashes. On the other hand, `str2` is appropriately sized at 15 characters, meaning it has enough room for its own string. The issue strictly lies with `str1`, not being large enough to hold the copied content from `str2`. Always make sure that your destination array is large enough to accommodate the source string plus the null terminator!