Problems I see:
Problem 1
You are not null terminating the concatenated string in comp
.
char* comp(char* s,char* t)
{
int count=0;
char *c;
c=s;
while(*s!='\0')
s++;
for(;*t!='\0';t++,s++)
*s=*t;
// Need this
*s = '\0';
return c;
}
Problem 2
You are calling the function incorrectly. You need to use:
i=comp(c, f); // Not comp(&c, &f)
Problem 3
The most serious problem is that you are writing over memory that you are not supposed to:
When you use:
char c[]= "hello there";
char f[]="world";
c
has enough memory to hold the string "hello there"
and f
has enough memory to hold the string "world"
. Trying to write beyond those limits is cause for undefined behavior. You could use:
char c[100]= "hello there";
char f[]="world";
i = comp(c, f);
That will be OK since c
has enough space for to hold the concatenated string.
Update, in response to OP's comment
char c[]= "hello there";
is equivalent to:
char c[12] = {'h', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e', '\0'};
Appending "world"
to that is equivalent to doing the following:
c[11] = 'w';
c[12] = 'o';
c[13] = 'r';
c[14] = 'l';
c[15] = 'd';
c[16] = '\0';
That is cause for undefined behavior since you are writing over elements of the array using out of bounds indices. Even accessing elements of the array using out of bounds indices is cause for defined behavior.