Wednesday, October 18, 2006

Concatenation of Pointers

Here is a little program/function to let you concatenate pointers. In this example it concatenates strings specifically.

#include

void concat( char *, char *, char * );

/* this functions copies the strings a and b to the destination string c */
void concat( char *a, char *b, char *c)
{
while( *a ) { /* while( *c++ = *a++ ); */
*c = *a; ++a; ++c;
}
while( *b ) {
*c = *b; ++b; ++c;
}
*c = '\0';
}

main()
{
static char string1[] = "Bye Bye ";
static char string2[] = "love.";
char string3[20];

concat( string1, string2, string3);
printf("%s\n", string3);
}

http://ftp.tuwien.ac.at/languages/c/programming-bbrown/c_104.htm

No comments: