I am very new in C language. I was trying to use strcat function.
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[]) {
char s1[] = "12345";
char s2[] = "abcde";
strcat(s1, s2);
puts(s1);
puts(s2);
return 0;
}
This one ran normally,but
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[]) {
char* s1 = "12345";
char* s2 = "abcde";
strcat(s1, s2);
puts(s1);
puts(s2);
return 0;
}
the last one failed to return a result. Why did the two different ways of declaration return different results in the strcat function. Thanks in advance.
In C the function
strcat
does not create a new character array containing concatenated strings. It appends characters from the second string to the first string of the first character array provided that it has enough elements to store the new characters. Otherwise the function will try to overwrite the memory beyond the character array that results in undefined behavior.So a valid use of the function in the first program can look the following way
In this program the character array is declared as having 11 elements. Thus it is able to accommodate the appended string
"abcde"
.In the second program there is an attempt to modify the string literal pointed to by the pointer
s1
. String literals in C and C++ are immutable. Any attempt to change a string literal results in undefined behavior even though in C opposite to C++ string literals have types of non-constant character arrays.From the C Standard (6.4.5 String literals)
So in the second program you again need to use a character array with enough elements. For example
Or you could use either a Variable Length Array (VLA) if the compiler supports them or dynamically allocate an array. For example
Or
Both of the code snippet invoke undefined behavior. In the first snippet
s1
has not enough space to hold any other character than six characters.In the second snippet you are trying to modify a string literal. Any attempt to modify a string literal lead to undefined behavior.
Read
strcat
man page: