0

How does strcpy() works in the code below. Why no Runtime Error ?

Can anyone explain how the following C code works ? Correct Answer : E Question : #include<stdio.h> #include <string.h> void main() { char string[14] = "i love coding"; char * pointer = string ; pointer += 7 ; strcpy(pointer,"programming"); printf("%s",string); } Options : A) i love B) i love coding C) Compilation Error D) programming E) i love programming F) Runtime Error

25th Jun 2019, 5:53 PM
cool_boy12344
cool_boy12344 - avatar
2 Réponses
+ 4
pointer - - > first index pointer+=2 - - > 2nd index pointer +=7 - - > 7th index (this is saying start from the 7th index) strcpy(pointer,"programming"); the strcpy just copy the string to the contiguous memory which has been allocated and go beyond if need be, because it was not instructed not to
25th Jun 2019, 10:13 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 2
Strcpy does not give warning or runtime error in C. Due to unchecked memory access in C the strcpy has no info about pointer that you have declared as in from which memory address location. It's better to use strncpy instead to avoid overlaps
25th Jun 2019, 6:57 PM
Sai Krish
Sai Krish - avatar