It's going to concatenate the strings every loop, not just when you want it to. I'm guessing that's causing buffer overrun which is overriding your str pointer and causing your program to crash.
Use...