Okay, you're still yelling in your original post (using all capital letters), and I'm not 100% sure what you're after, but here are some problems I noticed and some (strong) suggestions:
1. Declare main to explicitly return an int.
2. Don't cast the result of a malloc/calloc/realloc.
3. Don't increment p as you go along, you lose the starting address of the memory you allocated and free(p) will take a big ugly dump all over everything. Try using buf as the result of malloc/realloc and using an integer to index into buf (you can't just increment p since realloc doesn't guarantee the memory starts at the same location).
4. Don't use the same pointer for realloc, since it may return NULL, then you lose your originally allocated memory too.
5. Calling realloc, increasing 1 byte at a time is very inefficient. Either keep track of the size of what you've allocated, and when you reach the end, allocate a big chunk more, like:
Code:
if (num_of_chars_read == size_allocated) {
temp = realloc(p, size_allocated + 1000;
if (!temp) {
printf("ERROR: ran out of memory on the heap!\n");
return 1;
}
p = temp;
size_allocated += 1000;
}
6. You keep setting p to the start of the reallocated memory, which means as you write into p, you're only writing the first character each time.
7. You're forcing your buffer to start with 'y'. I don't think that's necessary or a good idea. Try a do-while loop.
8. You never increment count when you printf(..., p+count), you're always printing *(p+0) or just *p.
That's all I have time for right now. Again, I'm not totally clear on your problem description, and if you aren't either, we're both going to have serious problems with this program.