When allocating Strings on the heap (with ‘malloc’),
and initializing them with the standard input (using ‘fgets),
an unnecessary newline appears between them when trying to print them (with ‘printf’ and %s formatting).
for example:
main()
{
char *heap1;
char *heap2;
heap1=malloc(10);
heap2=malloc(10);
fgets(heap1,10,stdin);
fgets(heap2,10,stdin);
printf("%s%s",heap1,heap2);
}
with input “hi1\nhi2” produces “hi1\nhi2”.
compiled using gcc 4.3.4 for Debian.
fgets also reads the ‘\n’ (newline) character. You should remove it if you don’t want it to print like that.
after you read the contents of heap1.