strcat
- Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.
- 后附 src 所指向的空终止字节字符串的副本到 dest 所指向的空终止字节字符串的结尾。字符 src[0] 替换 dest 末尾的空终止符。产生的字节字符串是空终止的。
-
若目标数组对于 src 和 dest 的内容以及空终止符不够大,则行为未定义。
-
若字符串重叠,则行为未定义。
-
若 dest 或 src 不是指向空终止字节字符串的指针,则行为未定义。
char * strcat ( char * destination, const char * source );
Parameters
destination
- Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.
- 指向要后附到的空终止字节字符串的指针
source
- C string to be appended. This should not overlap destination.
- 指向作为复制来源的空终止字节字符串的指针
Return Value
- destination is returned.
- 该函数返回一个指向最终的目标字符串 dest 的指针。
Example
1// 2// Created by zhangrxiang on 2018/2/3. 3// 4 5#include <stdio.h> 6#include <string.h> 7 8int main() { 9 char str[] = "Hello C"; 10 char str2[50] = "Hello World"; 11 printf("%s\n", str);//Hello C 12 printf("%s\n", str2);//Hello World 13 char *str3 = strcat(str2, str); 14 printf("%s\n", str2);//Hello WorldHello C 15 printf("%s\n", str3);//Hello WorldHello C 16 str3 = "hi everyone"; 17 printf("%s\n", str3);//hi everyone 18 printf("%s\n", str2);//Hello WorldHello C 19 str2[2] = 'L'; 20 str[2] = 'L'; 21 str[0] = 'h'; 22 printf("%s\n", str2);//HeLlo WorldHello C 23 printf("%s\n", str);//heLlo C 24 25 char *str4 = "hi world"; 26// str[0] = 'H'; //行为为定义 h 27 printf("%c\n",str4[0]);//h 28 printf("%s\n",str4); 29 30 char str5[80]; 31 strcpy (str5,"these "); 32 strcat (str5,"strings "); 33 strcat (str5,"are "); 34 strcat (str5,"concatenated."); 35 puts (str5); 36 //these strings are concatenated. 37 38 return 0; 39}