c - How to use strcat() function? -


i new in c language. trying 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 1 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 1 failed return result. why did 2 different ways of declaration return different results in strcat function. in advance.

in c function strcat not create new character array containing concatenated strings. appends characters second string first string of first character array provided has enough elements store new characters. otherwise function try overwrite memory beyond character array results in undefined behavior.

so valid use of function in first program can following way

#include <stdio.h> #include <string.h>  int main(int argc, const char *argv[]) {     char s1[11] = "12345";     char s2[] = "abcde";      strcat(s1, s2);      puts(s1);     puts(s2);     return 0; }  

in program character array declared having 11 elements. able accommodate appended string "abcde".

in second program there attempt modify string literal pointed pointer s1. string literals in c , c++ immutable. attempt change string literal results in undefined behavior though in c opposite c++ string literals have types of non-constant character arrays.

from c standard (6.4.5 string literals)

7 unspecified whether these arrays distinct provided elements have appropriate values. if program attempts modify such array, behavior undefined.

so in second program again need use character array enough elements. example

#include <stdio.h> #include <string.h>  int main(int argc, const char *argv[]) {     char s1[11] = "12345";     char* s2 = "abcde";      strcat(s1, s2);      puts(s1);     puts(s2);     return 0; } 

or use either variable length array (vla) if compiler supports them or dynamically allocate array. example

#include <stdio.h> #include <string.h>  int main(int argc, const char *argv[]) {     char *s1 = "12345";     char* s2 = "abcde";     char s3[strlen( s1 ) + strlen( s2 ) + 1];          strcpy( s3, s1 );     strcat( s3, s2 );      puts(s1);     puts(s2);     puts(s3);      return 0; } 

or

#include <stdio.h> #include <string.h> #include <stdlib.h>  int main(int argc, const char *argv[]) {     char *s1 = "12345";     char* s2 = "abcde";     char *s3 = malloc( strlen( s1 ) + strlen( s2 ) + 1 );          if ( s3 != null )     {         strcpy( s3, s1 );         strcat( s3, s2 );          puts(s1);         puts(s2);         puts(s3);     }      free( s3 );      return 0; } 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -