/********************************************************************* appnd.c Sample program written by Jose Mari Reyes copyright(c)1999 by Jose Mari Reyes www.angelfire.com/co2/xtechnica This program dynamically appends a string pointer by using ANSI malloc and realloc **********************************************************************/ #define TEST #include int slen(char *str) { int len = 0; while (str[len]) len++; return len; } void append_str(char **val, char *src) { int len = 0; int i = 0; char *pt; pt = *val; if ((len = slen(pt)) < 1) { pt = malloc (slen(src) * (sizeof(char) * 2)); while (src[i]) { pt[i] = src[i]; i++; pt[i] = '\0'; } } else { len = slen(pt); pt = realloc ( pt, len * ( sizeof(char) + 10)); while (src[i]) { pt[len] = src[i]; i++; len++; pt[len] = '\0'; } } *val = pt; } #ifdef TEST #include #include void main() { char *pt; char buff[90]; strcpy(buff," I saw her today, I saw here face, it was the face I love\n"); append_str(&pt, buff); append_str(&pt, "\nAnd I knew I had to run away, and beg down on my knees and pray\n"); strcpy(buff,"\nThat they go away, still they begin, those needles and pins..\n"); append_str(&pt, buff); strcpy(buff,"\nBecause of all my pride, The tears I got to hide...\n"); append_str(&pt, buff); strcpy(buff,"\nOh I thought I was smart, I won her hearth, did'nt think I do\n"); append_str(&pt, buff); strcpy(buff,"\nFor now I see, She's worse to him than me, let her go ahead\n"); append_str(&pt, buff); strcpy(buff,"\n- The Searchers , cool band 123456789"); append_str(&pt, buff); strcpy(buff,"\n\n1uenk3400sdkfgQW3ldjjc$@#$WDFgmm34XDG#$F\n\n"); append_str(&pt, buff); strcpy(buff,"\n- Now let's look and listen for this cool and new"); append_str(&pt, buff); strcpy(buff,"\n\nHardcore Baby!!! so come on, so baby come on!!!! yeah\n"); append_str(&pt, buff); printf("\n%s length(%d)",pt, strlen(pt));getchar(); free(pt); } #endif