So, it seems I have a bug in my code:
What I'm trying to do is insert "abc" in the 3rd position of "onetwo". Results would be "oneabctwo", but all I get is crashing.Code:char *mem = malloc(9), *bottom = malloc(3), *in = "abc"; int position = 2, i; mem = "onetwo"; printf("OLD mem = %s\n", mem); /* save the second half */ for(i = position; i < (9 - position); i++) { bottom[i] = mem[i + position]; position++; } /* insert the middle */ for(i = position; i < (position + 3); i++) { mem[i] = in[i]; } /* put it back together */ for(i = (position + 3); i < 9; i++) { mem[i + position] = bottom[i]; } printf("NEW mem = %s\n", mem);
There's got to be a better way to do this, I'm not sure.
Thank you for your suggestions,
Congi