fool said:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
char *s=0,ch;
int n=0;
do
{
s = realloc(s, n);
if(!s)
exit(EXIT_FAILURE);
This is not a good idea, but it'll do for now, except for the fact that
you're allocating ZERO bytes of memory on the first time round.
fgets(s, sizeof s, stdin);
sizeof s is wrong - s has type char *, so sizeof s is equivalent to
sizeof(char *), which isn't going to change any time soon. What you should
put here instead is the number of bytes available at the memory pointed to
by s - in this case, that would be n.
What if fgets failed? (It returns NULL if it encounters an error or an
end-of-file condition.) You need to detect this.
copies first character of the input string to ch
copies ch to the (n-1)th character of s - is that *really* what you want to
do? Especially if it overwrites your terminating null character (which
seems possible to me, although I admit I haven't read the code very
closely).
printf("%s",s);
}while(ch != 0 && (*s) != '\0'); /* derefer s and till s
reaches '\0', correct?*/
I think you need to take a step back and decide exactly what it is that
you're trying to do. Do you just want to read one string from stdin, or
many? Do you want to store one string, or many? What are you aiming for? If
you can answer this, perhaps we can help you better.