You can't cast the result of MCStringGetCString() as a char*: if it returns a const char*, it's because the string referenced by the MCStringRef shall never be modified but by MCString* functions - which ensures the MCStringRef is mutable before executing any modification on it; otherwise, it would change the content of all the references to that string. What you want to do instead is to create a MCNativeCharArray from the MCStringRef, modify that array in the loop, and then create a new MCStringRef from it after the loop.