FWIW, a pointer to pointer might help you keep track of freeing memory where it is allocated, even if you previously allocated something.
Code:
#include <stdlib.h>
#include <string.h>
#include <stddef.h> /** for NULL and size_t **/
/** Concatenate string a to string b and store the result **/
char * concat ( char ** result, const char * a, const char * b )
{
char * more = NULL;
if ( a && b )
{
size_t alen = strlen( a );
size_t blen = strlen( b );
more = realloc( *result, alen + blen + 1 );
if ( more )
{
strcpy( more, a );
strcat( more, b );
*result = more;
}
}
return more;
}
int main ( void )
{
char * p = NULL;
if ( concat( &p, "Hello, ", "world." ) )
{
puts( p );
free( p );
}
return 0;
}