sonu said:
int main(void)
is much better style.
{
char *ptr1,*ptr2;
ptr1=(char*)malloc(sizeof(ptr1));
You've just masked a bug. You did not include <stdlib.h>, and have cast
the return value of `malloc`. Without prototype (in <stdlib.h>) the
compiler has to assume `malloc` returns an `int`, which you then force
into a `char *`. *Never* cast the return value of `malloc`.
Aside from that, you allocate the space for `ptr1` the size of the
pointer to `char` on your implementation. Is this really what you
wanted? Probably not, as you then go on and input "aaa bbb ccc ddd eee
fff ggg" which is most likely much bigger.
Congratulations! You've just created a buffer overflow vulnerability.
gets(ptr1);
ptr2=(char*)malloc(sizeof(ptr2));
Exactly the same problems as above. You manage not to overflow your
buffer (assuming sizeof(char *) >= 4, which is reasonable on modern
hosted implementations), as you input "kkk", which is 4 bytes long
(remember that pesky terminating \0).
gets(ptr2);
printf("%s",ptr1);
printf("%s",ptr2);
Not terminating `printf` with `\n` (or doing `fflush(stdout)`) may
result in nothing at all being output (output is line buffered -- no
end of line, no output, maybe).
}
suppose ptr1= aaa bbb ccc ddd eee fff ggg
&
ptr2=kkk
output is :-aaa bbb ccc & kkk
why its not printing complete the first string
See comments above. BTW, I'm surprised you get the output you claim you
do.
Pls any one help me bcoz i need it Urgent
The only urgent things for you right now would be to go back to your C
text book.
Someone may be able/willing to help you further if you specify exactly
what the code above is supposed to achieve.