I make a test case about putting input to a char pointer. When I try to run this program, the output isn't right(it becomes a series of random characters like : _ @$).I intend to print each element in that char pointer. I make some changes on my code, but it's still wrong(same problem as before). Could someone help me to figure out what's going wrong and the way to fix it?
int chara;
int counts =0;
main(){
char *buffer=(char *)malloc(sizeof(char)*25);
while((chara=getchar())!= EOF&& counts<25){
*buffer++ = chara;
printf("%c\n",*buffer);
counts++;
}
*buffer = '\0';
printf("%s\n",buffer);
free(buffer);
}