It does work perfectly. Anything may happen with UB and that includes correct operation!
Try the static analyzer though - which goes bonkers and gives incorrect warnings ...
It thinks y points to a string literal (so cant be written to) and doesn't realize that *(y+o) or y[o] point to something else which is OK for writes.
It also assumes that since o has not been written to, the array s is used uninitialized when passed to puts().
Code: Select all
gcc -Wall -Wextra -fanalyzer -O3 try.c -o try In function 'dogcopy', inlined from 'main' at try.c:11:5: try.c:5:14: warning: write to string literal [-Wanalyzer-write-to-string-literal] 5 | do *(y+o)=*y; | ~~~~~~^~~ 'main': event 1 | | 11 | dogcopy(s,"scratch woof meow bark"); | | ^ | | | | | (1) inlined call to 'dogcopy' from 'main' | +--> 'dogcopy': event 2 | | 5 | do *(y+o)=*y; | | ~~~~~~^~~ | | | | | (2) write to string literal here | try.c: In function 'main': try.c:12:5: warning: 's' is used uninitialized [-Wuninitialized] 12 | puts(s); | ^~~~~~~ In file included from try.c:1: /usr/include/stdio.h:632:12: note: by argument 1 of type 'const char *' to 'puts' declared here 632 | extern int puts (const char *__s); | ^~~~ try.c:10:10: note: 's' declared here 10 | char s; | ^