Let’s say I have a char *example that contains 20 chars. I want to remove every char from example[5] to example[10] and then fix up the array so that example[11] comes right after example[4].
Basically shifting all the characters after the deleted region to when the deleted region started.
Any ideas?
EDIT: I think there might be a way using memcpy? But I’m not sure how to do it.
You can’t use
memcpy()reliably because there’s overlap between the source and target; you can usememmove(). Since you know the lengths, you use:Remember you need to copy the null terminator too.
Compiled with a C99 compiler, that yields:
If you have a C89 compiler (more specifically, C library), you’ll have to worry about the
zin the format string, which indicates asize_targument. It’s simplest to remove thezand cast the result ofstrlen()with(unsigned).