-4

I know that the program should put string2 and string1 after the string3, but I don't know where the problem is.

#include <stdio.h>

char* f(char *p1, char *p2, char *p3)
{
    char* tp1 = p1;
    char* tp2 = p2;
    char* tp3 = p3;

    while (*tp1)
        tp1++;

    while (*tp2)
        tp2++;

    while (tp1>p1)
        *tp3++ = *--tp1;

    while (tp2>p2)
        *tp3++ = *--tp2;

    *tp3=0;
    return p3;
}

int main(void)
{
    char* string1="abcd";
    char* string2="efg";
    char* string3="\0";

    char* ris=f(string1,string2,string3);

    printf("%s\n", ris);
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 3
    You are trying to modify *string literals* - that is *undefined behavior* – UnholySheep Sep 07 '22 at 09:25
  • 1
    Also `char* string3="\0";` does not point to memory large enough to concatenate your 2 other strings into – UnholySheep Sep 07 '22 at 09:26
  • If you want a good answer you need at least a good title for your question. – Mike Sep 07 '22 at 09:32
  • 1
    The string literals at string1 and string2 are OK, you're just reading them. But you're trying to concatenate both into a string literal at string3 (read-only). And only one byte is available at string3. – Paul Lynch Sep 07 '22 at 09:48

1 Answers1

-1

The function tries to concatenate two strings in the reverse order and store the result in a character array.

However there is a problem because there is passed a string literal as the destination array to the function

char* string3="\0";

that may not be changed. Any attempt to change a string literal results in undefined behavior.

Instead you should write for example

char string3[8];

That is you need to pass a large enough array that can accommodate two concatenated strings: string1 and string2.

Also the function should be declared at least like

char* f(const char *p1, const char *p2, char *p3);

That is the passed string except of the result array are not changed within the function.

Here is your updated program. I changed the order of parameters in the function and renamed it.

#include <stdio.h>

char * reversed_concatenation( char *s1, const char *s2, const char *s3 )
{
    char *p1 = s1;
    
    const char *p2 = s2;

    while ( *p2 ) ++p2;

    while ( p2 != s2 )
    {
        *p1++ = *--p2;
    }

    const char *p3 = s3;

    while ( *p3 ) ++p3;

    while ( p3 != s3 )
    {
        *p1++ = *--p3;
    }

    *p1 = '\0';

    return s1;
}

int main( void ) 
{
    const char *string1 = "abcd";
    const char *string2 = "efg";
    char string3[8];

    puts( reversed_concatenation( string3, string1, string2 ) );
}

The program output is

dcbagfe

In this function for example this for loop

    const char *p2 = s2;

    while ( *p2 ) ++p2;

finds the end of the first concatenated string and then this for loop

    while ( p2 != s2 )
    {
        *p1++ = *--p2;
    }

writes the string in the reverse order in the result character array.

The same operations are done with the second concatenated string.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335