Originally Posted by
tarunjain07
b is collecting the pointer returned from function
yes no output is something wrong with code ??
At the end of your function when you return b ... b is pointing at the null on the end of your string.
Not only that, but the minute you did b++ you orphaned the memory you reserved for the string, causing a "memory leak" where free() will fail to release it.
You need to preserve your pointers in their original locations or you lose your strings...
Code:
char * copy(char*s)
{ char *b=malloc(strlen(s) + 1); // allow for trailing null
int i = 0;
do
{ b[i]=s[i];
i++; }
while(s[1])
return b; }
Then in main you need to release the memory you've reserved...
Code:
int main(void)
{
char *a="Hello Tarun";
char *b = NULL;
b = copy(a);
puts(b);
free(b);
return 0;
}