The problem is that s1 points to a string literal, and you are trying to change it by adding s2 to it. You are not allowed to modify string literals. You need to create an array of characters and copy both lines into it, for example:
char *s1 = "Hello, "; char *s2 = "world!"; char s3[100] = ""; strcat(s3, s1); strcat(s3, s2); printf("%s", s3);
โLarge enoughโ means at least strlen(s1) + strlen(s2) + 1 . + 1 must take into account the zero limiter.
To be told, you should seriously consider using strncat (or perhaps the best, but non-standard strlcat , if available), which are checked against borders and therefore far outperform strcat .
James McNellis
source share