This produces a segmentation fault unless the third argument to memmove is 0.
Why? I tried figuring this out myself but it doesn't make sense. I've tried it on Linux and on windows and it didn't work on either :l
Never mind, it was the way I was using char* instead of "char myChar[]".
str is pointing to a location of memory which you cannot modify.
Change line 6 to char str[] = "Memmove is cool"; so you'll have an array which you can modify
Bazzy is right.
Always remember diff betn array [] and pointer *
Array: you can modify any single memory location within array.
Pointer: You can change the memory location to which the pointer is pointing to.
Reverse is not true.
char* str = "Memmove is cool" creates a pointer to a constant character string char str[] = "Memmove is cool" creates a pointer to a mutable character string
To prevent these mistakes in the future, always assign string literals to const char *. The compiler will be able to catch the error because memmove() and other C functions from cstring take a 'char *'. 'const char *' cannot be implicitly converted to 'char *', but 'char *' can be implicitly converted to 'const char *'.
EDIT: Oh, and the reason char str[] is modifiable and const char *str isn't is because the former is syntactic sugar for char str[] = {'M','e','m','m', /*(...)*/,0};
which implies copying an array to the stack at run time, while the latter makes a direct pointer to the string literal, and string literals are loaded to read-only memory.