Can someone explain to me how s2 becomes "ice" at the end? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Can someone explain to me how s2 becomes "ice" at the end?

#include <stdio.h> #include<string.h> int main() { char s1[]="Short Message Service",*s2,*s3; s2=strchr(s1,'M'); puts(s1); puts(s2); puts(s3); s3=strrchr(s2,'S'); puts(s1); puts(s2); puts(s3); strncpy(s1+1,s2,1); puts(s1); puts(s2); puts(s3); strcpy(s1+2,s3); puts(s1); puts(s2); puts(s3); return 0; }

14th Jun 2022, 6:12 PM
Sofija
Sofija - avatar
1 Resposta
+ 1
Short Message Service\0 SMort Message Service\0 SMService\0age Service\0 At the start, the pointer s2 points to the 7th character, M. When the last strcpy executes, it overwrites the 3rd character through the 10th with the word "Service" plus terminating null. At the 7th character, s2 now points to the 'i' in "ice\0".
15th Jun 2022, 12:45 AM
Brian
Brian - avatar