I have a program like this(x86_64 GNU/Linux)
int main()
{
char s[] = "123456789";
char d[] = "123";
strcpy(d, s);
printf("%p, %0p\n", s, d);
printf("%s, %s", s, d);
return 0;
}
and the output is : 0xeb6d2930 0xeb6d2910 123456789 123456789 I am little confused with the result I think the program in the memory is like this:
- '9','\0' . .
- '5','6','7','8'
- 0x7fff813af310: '1','2','3','4'
- 0x7fff813af300: '1','2','3','\0'
so the result should be *s = "789", *d = "123456789"
could you guys explain why the result isn't as I thought? I changed the format specifier to %p to print the address of d and s I know that s and d are overlapped so there is not enough space for d to hold s which may lead to undefined behaviour,but anyway I was wondering why the result is *s = "123456789" *d = "123456789"