I had issue in below mention program to assign a value to the pointer.String concatenation Program( *s1=*s2) [closed]

So you are getting access voilation….

The problem is that your buffers are probably overflowing and you have no length check in your code…

The buffers you have allocated are only 25 bytes long

char str1[25], str2[25];

so to intruduce a length check, add an extra parameter to concat which tells how long the output buffer is, like this

void concat(char *s1, char *s2, int len){
    // Checking Null character
    while (*s1 != '\0' && len > 0)
        s1++, len--;
    //Checking Null character
    while (*s2 != '\0' && len > 0)
    {
        *s1 = *s2; //<-- Getting error in this line        
        s1++;
        s2++;
        len--;
    }
    if (len > 0) // only set the null terminator if we have space
       *s1 = '\0';
}

an then call it as this

concat(str1, str2, 25);

An then also read the man page for strncat

Leave a Comment