1

How do I achieve concatenation using only pointers since the code below gives (a segmentation fault) error (at runtime)?

#include <stdio.h>
#include <string.h>
int main()
{
    char *s1="Front";
    char *s2="Back";
    char *s3=strcat(s1,s2);
    puts(s3);
    return 0;
}
0

5 Answers 5

4

Because you are trying to write to a string literal. The line char *s1 = "Front"; points to a string constant, which can't be written to.

Change it to char s1[20] = "Front"; and it should work out like you expect - as long as you are adding no more than 14 characters.

Sign up to request clarification or add additional context in comments.

Comments

1

Read about what strcat does, keeping in mind that literal strings (such as "Front" and "Back") are not modifiable.

Comments

1

The code gives an error because concatenating s1 with s2 will require the allocated memory for s1 to be >= the combined lengths of the two byte arrays. strcat will attempt to iterate over memory that wasn't assigned to s1, thus causing a runtime error. The only way this will work through pointers is if you make str3 a char array where the size is known at compile time (or a pointer to a dynamically-allocated one for runtime):

char s1[10] = "Front";
char *s2 = "Back";

char *s3 = strcat(s1, s2);

Comments

1

You need space for the result, like this:

#include <stdio.h>
#include <string.h>
int main()
{
    char *s1="Front";
    char *s2="Back";
    char s3[80];
    s3[0] = '\0';
    strcat(s3,s1);
    strcat(s3,s2);
    puts(s3);
    return 0;
}

4 Comments

Why do you initialize s3[0]=0?? Sorry if it's a naive question!
strcat() adds data to the end of a null-terminated string; the s3[0] = '\0'; ensures that s3 is an empty null-terminated string before the concatenations start.
@JonathanLeffler - I believe my code would have compiled just fine. Don't feel like going to K+R to find the reference right now tho.
It would, but it is clearer to use '\0' to mean the null byte in character strings and reserve 0 for bigger integers. Your choice; you can return it to what you wrote.
0

You can do it this way too. Though it is not better than strcat() but it is implemented using pointers only.

#include<stdio.h>
#define SIZE 20
void concat(char *,char *);

int main()
{
    char string1[SIZE]="\0",string2[SIZE]="\0";

    printf("Enter String 1:\n");
    gets(string1);
    printf("Enter String 2:\n");
    gets(string2);

    concat(string1,string2);

    return 0;
}
void concat(char *str1,char *str2)
{
    char *conc=str1;

    while(*str1!='\0')
        str1++;

    *str1=' ';
    ++str1;

    while(*str2!='\0')
    {
        *str1=*str2;
        str1++,str2++;
    }

    *str1='\0';
    printf("Concatenated String:\n");
    puts(conc);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.