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 a
You can't use memcpy() reliably because there's overlap between the source and target; you can use memmove(). Since you know the lengths, you use:
memmove(&example[5], &example[11], (20 - 11 + 1));
Remember you need to copy the null terminator too.
#include
#include
int main(void)
{
char array[] = "abcdefghijklmnopqrst";
char *example = array;
printf("%.2zu: <<%s>>\n", strlen(example), example);
memmove(&example[5], &example[11], (20 - 11 + 1));
printf("%.2zu: <<%s>>\n", strlen(example), example);
return(0);
}
Compiled with a C99 compiler, that yields:
20: <>
14: <>
If you have a C89 compiler (more specifically, C library), you'll have to worry about the z in the format string, which indicates a size_t argument. It's simplest to remove the z and cast the result of strlen() with (unsigned).