It looks like you're assuming that malloc() initializes all the entries in the array table1 to nonzero values and puts a zero at the end. In fact, you can't assume anything about the values in the array before you initialize them. Same for table2.Code:while(*table1)
Edit: Probably you work with C strings a lot and forgot this wasn't one?