
「官网地址0365.tv」-「永久地址0365.tv」
将两个char类型连接。
例如:
char d[20]="Golden";
char s[20]="View";
strcat(d,s);
//打印d
printf("%s",d);
输出 d 为 GoldenView (中间无空格)
d和s所指内存区域不可以重叠且d必须有足够的空间来容纳s的字符串。
返回指向d的指针。
extern char *strcat(char *dest, const char *src);
#include <string.h>
在C中,函数原型存在 <string.h>头文件中。
在C++中,则存在于<cstring>头文件中。
把src所指向的字符串(包括“\0”)复制到dest所指向的字符串后面(删除*dest原来末尾的“\0”)。要保证*dest足够长,以容纳被复制进来的*src。*src中原有的字符不变。返回指向dest的指针。
src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
程序执行结果为:
GoldenGlobalView
Strcat函数原型如下(以下代码为错误代码,想要通过char *指针修改字符串常量中的字符会导致Segment fault错误):
strcat 即 Strings Catenate,横向连接字符串。
combinedStr= strcat(s1, s2, ..., sN)
将数组 s1,s2,...,sN 水平地连接成单个字符串,并保存于变量 combinedStr中。如果任一参数是元胞数组,那么结果 combinedStr 是一个元胞数组,否则,combinedStr是一个字符数组。
>> a = 'Hello'
a =
Hello
>> b = ' Matlab'
b =
Matlab
>> c = strcat(a,b)
c =
Hello Matlab
For character array inputs, strcat removes trailing ASCII white-spacecharacters: space, tab, vertical tab, newline, carriage return, and form-feed. To preserve trailing spaces when concatenating character arrays, use horizontal array concatenation, [s1, s2, ..., sN]. See the final example in the following section.
For cell array inputs, strcat does not remove trailing white space.
When combining nonscalar cell arrays and multi-row character arrays, cell arrays must be column vectors with the same number of rows as the character arrays.