[ale] C and segmentation fault

Christopher Hamilton chrish at ifsintl.com
Thu Jul 17 16:30:05 EDT 1997


>main()
>{
>    char s1[BIGENOUGH];
>    char s2[WHATEVER];
>
>    strcpy(s1, "S1");
>    strcpy(s2, "S2");
>    strcat(s1,s2);
>
>    printf("s1: %s\ns2: %s\n", s1, s2);
>}

If you're going this way, but need the ability to easily append extra
characters to a string (e.g. s1), to go along with the initial mail
using pointers, you can try using a couple of macros that I wrote to
help me out a bit:

#include <stdlib.h>  /* required for malloc/realloc */

#define STRCPY(d,s) d=(char *)malloc(strlen(s)+1);strcpy(d,s)
#define STRCAT(d,s) d=(char *)malloc(d,strlen(d)+strlen(s)+1);strcat(d,s)

main()
{
   char *s1,*s2;

   STRCPY(s1,"S1");
   STRCPY(s2,"S2");
  
   STRCAT(s1,s2);
 
   printf("%s\n",s1);
}


---
Christopher Hamilton    Internal System Administrator
chrish at ifsintl.com      IFS International, Inc.






More information about the Ale mailing list