Actually, the problem is that you're passing a pointer to a function, and then inside the function, trying to change what the pointer points to. You can't do that. If you need to change what a pointer points to, you have to pass a pointer to that pointer. Thus, it should be:
Code:
void H_strCat (char **str1, char *str2){
...stuff...
}
For those disbelievers out there, try it yourself:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void re( char *s )
{
s = realloc( s,strlen( s ) * 2 );
strcpy( s, "Foo!!" );
printf("s is %s\n", s );
}
int main( void )
{
char *s;
s = malloc( strlen( "Hello World!" ) + 1 );
strcpy( s, "Hello World!" );
printf("s is %s\n", s );
re( s );
printf("s is %s\n", s );
return 0;
}
/*
s is Hello World!
s is Foo!!
s is
*/
Additionally, you're both using realloc incorrectly. You shouldn't ever directly assign the pointer returned from realloc to the memory you're allocating, in case it fails. Yes, I did it in the above example, but I was just illustrating what your code does.
This is the correct way to realloc:
Code:
void re( char *s )
{
char *ptr = realloc( s,strlen( s ) * 2 );
strcpy( s, "Foo!!" );
printf("s is %s\n", s );
s = ptr;
}
And looking at the last line, you should now be able to see what I was talking about earlier, and why this fails. It fails because you cannot assign a pointer to another inside a function and have it update the value outside the function.
Rather, you'd have to pass a pointer to a pointer, like so:
Code:
void re2( char **s )
{
char *ptr = realloc( *s, strlen( *s ) * 2 );
strcpy( ptr, "Foo!!" );
*s = ptr;
printf("*s is %s\n", *s );
}
Quzah.