-3

why does the following string concatenation does not work?

main()
{
char *str1 = "United";
char *str2= "Front";
char *str3;
str3 = strcat(str1, str2 ) ;
printf("\n%s",str3 );

}

I got this problem in exercise questions in one of a book on pointers. The question mentions

[Q] Is the code correct if not why and also correct the code.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    _...does not work..._ What does that mean? Why should it work and what should it do? What errors/issues are you facing? – B001ᛦ Jun 18 '19 at 09:15
  • 1) because the concatenation target `str1` does not have enough memory for the concatenation, and 2) because string literals such as `str1` should be treated as read-only. It would work with `char str1[12] = "United";` – Weather Vane Jun 18 '19 at 09:18
  • 1
    That very same book should tell you some details about string literals, pointers and how those string functions work. Did you try to combine these pieces of information? – Gerhardh Jun 18 '19 at 09:24
  • @B001ᛦ the main problem could be that he might not even face any error on some systems. Still the code is crap. – Gerhardh Jun 18 '19 at 09:26
  • Before you post, please consider searching the web for information. – babon Jun 18 '19 at 09:29
  • @B001ᛦ it's a question in a book, not a coding problem encountered by the OP. – Weather Vane Jun 18 '19 at 09:30

3 Answers3

1

See my answer to the question concatenation of character arrays in c

You may not change string literals.

This statement

str3 = strcat(str1, str2 ) ;

tries to change the string literal str1 and moreover tries to write beyond the string literal.

To make a concatenated string you have to allocate a memory large enough to contain the both strings.

What you need is the following

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) 
{
    const char *str1 = "United";
    const char *str2 = "Front";

    char *str3 = malloc( strlen( str1 ) + strlen( str2 ) + 1 );

    strcpy( str3, str1 );
    puts( strcat( str3, str2 ) );

    free( str3 );

    return 0;
}

The program output is

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

When you write char* s = "something" a piece of read-only memory is allocated. More on this here.

The declaration of strcat looks like this:

char *strcat( char *dest, const char *src );

Basically what it will do is append src to dest, but since your destination, str1 does not have enough memory to hold both strings.

So what I would do is, either use snprintf with a pre-allocated buffer or:

char *str1 = "United";
char *str2 = "Front";

char *buf = calloc(strlen(str1) + strlen(str2) + 1, sizeof(char));

strncpy(buf, str1, strlen(str1));
strncat(buf, str2, strlen(str2));

printf("%s", buf);

Or with snprintf:

char *str1 = "United";
char *str2 = "Front";

int buf_len = strlen(str1) + strlen(str2) + 1;
char *buf = calloc(buf_len, sizeof(char));

snprintf(buf, buf_len, "%s%s", str1, str2);
Victor
  • 13,914
  • 19
  • 78
  • 147
  • `strncpy(dest, src, strlen(src));` is exactly equivalent to plain `strcpy(dest, src);`. There's really no point using `strncpy()` there. Oh, and `sizeof (char)` can only be 1, since it measures size in units of `char`. – Toby Speight Jun 18 '19 at 16:34
-1

The first parameter of strcat (str1 in your case) needs to have enough allocated memory to hold the concatenated string. You can either create it with malloc or declare it as an array with a big enough size.

MrPromethee
  • 721
  • 9
  • 18
  • since *str1 is a char pointer and now it holds the starting address of the string "united " why can't we add " front" next to its memory ? i – big brother Jun 18 '19 at 09:25
  • 1
    @bigbrother because you don't own any memory at this location to store the extra `char`s. – Gerhardh Jun 18 '19 at 09:27