##strcat函数原型
char * strcat ( char * destination, const char * source );
##strcat常见写法
1// main.cpp 2// 字符数组strcat()函数的使用 3// char * strcat ( char * destination, const char * source ); 4// 来源的头文件 #include <string.h> 或者#include <cstring> 5// 功能:将字符串 source连接到字符串 destination的后面,并把destination地址返回。 6// 常见问题:strcat()函数常见的错误就是数组越界,即两个字符串连接后,长度超过第一个字符串数组定义的长度,导致越界 7// Created by mac on 2019/4/5. 8// Copyright © 2019年 mac. All rights reserved. 9#include <iostream> 10#include <cstring> 11using namespace std; 12int main(int argc, const char * argv[]) { 13 //几种常见的写法: 14 // 写法一:直接不定义字符数组,定义两个字符指针。编译成功,运行出错。 15 // char *p="Hell"; 16 // char *q="o,World!"; 17 // strcat(p,q); 18 // cout<<p<<endl; 19 20 // 写法二:定义了字符数组,但是不指定字符数组的长度, 程序的编译运行都没有问题。 21 // char p[]="Hell"; 22 // char *q="o,World!"; 23 // strcat(p, q); 24 // cout<<p<<endl; 25 26 //写法三:定义字符数组的时候指定字符数组的大小,程序的编译运行都没有产生问题 27 // char p[30]="Hell"; 28 // char *q="o,World!"; 29 // strcat(p, q); 30 // cout<<p<<endl; 31 32 //写法四:定义没有指定长度的字符数组和字符串常量,编译运行都没有问题 33 // char p[]="Hell"; 34 // strcat(p,"o,World"); 35 // cout<<p<<endl; 36 //写法五:直接连接两个字符串常量 编译成功,运行出错。 37 //cout<<strcat("Hell", "o,World")<<endl; 38 return 0; 39} 40
##运行成功输出
<div align= " left "> <img src= "https://imgur.com/T6kXs7J.jpg " width = " 600" height = "350" alt= “运行成功输出” align=center> </div>##运行失败输出
<div align= " left "> <img src= "https://imgur.com/WR7t9gl.jpg " width = " 600" height = "350" alt= “运行失败输出” align=center> </div>##Tips
- 字符串连接的时候主要看destination中的空间是否充足。
##参考文献