Concatenating strings in C, which method is more efficient?

后端 未结 10 951
迷失自我
迷失自我 2020-11-28 19:51

I came across these two methods to concatenate strings:

Common part:

char* first= \"First\";
char* second = \"Second\";
char* both = malloc(strlen(fi         


        
10条回答
  •  悲哀的现实
    2020-11-28 20:42

    Neither is terribly efficient since both methods have to calculate the string length or scan it each time. Instead, since you calculate the strlen()s of the individual strings anyway, put them in variables and then just strncpy() twice.

提交回复
热议问题