strcat函数原型

char * strcat ( char * destination, const char * source );

strcat常见写法

//  main.cpp
// 字符数组strcat()函数的使用
// char * strcat ( char * destination, const char * source );
// 来源的头文件 #include <string.h> 或者#include <cstring>
// 功能:将字符串 source连接到字符串 destination的后面,并把destination地址返回。
// 常见问题:strcat()函数常见的错误就是数组越界,即两个字符串连接后,长度超过第一个字符串数组定义的长度,导致越界
// Created by mac on 2019/4/5.
// Copyright © 2019年 mac. All rights reserved.
#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, const char * argv[]) {
//几种常见的写法:
// 写法一:直接不定义字符数组,定义两个字符指针。编译成功,运行出错。
// char *p="Hell";
// char *q="o,World!";
// strcat(p,q);
// cout<<p<<endl; // 写法二:定义了字符数组,但是不指定字符数组的长度, 程序的编译运行都没有问题。
// char p[]="Hell";
// char *q="o,World!";
// strcat(p, q);
// cout<<p<<endl; //写法三:定义字符数组的时候指定字符数组的大小,程序的编译运行都没有产生问题
// char p[30]="Hell";
// char *q="o,World!";
// strcat(p, q);
// cout<<p<<endl; //写法四:定义没有指定长度的字符数组和字符串常量,编译运行都没有问题
// char p[]="Hell";
// strcat(p,"o,World");
// cout<<p<<endl;
//写法五:直接连接两个字符串常量 编译成功,运行出错。
//cout<<strcat("Hell", "o,World")<<endl;
return 0;
}

运行成功输出

运行失败输出

Tips

  • 字符串连接的时候主要看destination中的空间是否充足。

参考文献

CPP strcat函数使用的更多相关文章

  1. C语言strcat()函数:连接字符串

    头文件:#include <string.h> strcat() 函数用来连接字符串,其原型为:    char *strcat(char *dest, const char *src); ...

  2. strcat函数的使用需要注意的问题

    曾被这个函数困扰了好久,然后各种假设,验证:但是最后却发现这个函数并没有什么好讲的,原来的过错一切都源于忽略了“*dst去掉\0,然后加上*src,最后返回*dst”这句话的真正含义:给*dst分配的 ...

  3. strcat()函数常见问题

    strcat(char *_Destination,const char *_Source)函数的功能是将后一个字符串粘贴到前一个字符串的末尾 原型 char *strcat(char *_Desti ...

  4. strcat函数造成的段错误(Segmentation fault)

    转自:http://book.51cto.com/art/201311/419441.htm 3.21  strcat函数造成的段错误 代码示例 int main() { char dest[7]=& ...

  5. 【C语言】模拟实现库函数strcat函数

    //模拟实现库函数strcat函数 #include <stdio.h> #include <string.h> #include <assert.h> char ...

  6. strcat函数的坑点

    我们先看下面这样一段代码: #include <iostream> #include <stdlib.h> using namespace std; int main() { ...

  7. 编写一个程序实现strcat函数的功能

    写自己的strcat函数------→mycat #include <stdio.h> #include <string.h> #define N 5 char *mycat( ...

  8. C语言中strcpy,strcmp,strlen,strcat函数原型

    //strcat(dest,src)把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0' char *strcat(char * strDest, const char ...

  9. 由strcat函数引发的C语言中数组和指针问题的思考

    问题一 首先,来看一下下面这段代码: #include <stdio.h> #include <string.h> int main() { char *str = " ...

随机推荐

  1. Android 应用安装

    DDMS下Files Explorer /data/app/xxx.apk 安装过程:1.拷贝文件xxx.apk到/data/app/xxx-1.apk 2.在/data/data目录下创建一个文件夹 ...

  2. CSS深入理解之absolute(HTML/CSS)

    absolute和float是同父异母的兄弟,因为它们具有相同点:包裹性与破坏性 absolute的特点 1.独立的,并且可以摆脱overflow的限制,无论是滚动还是隐藏: 2.无依赖,不受rela ...

  3. python汉字转拼音

    上代码: #!/usr/bin/env python # -*- coding:utf-8 -*- """ Author:cleverdeng E-mail:clverd ...

  4. 如何在定制化组件中实现并使用v-model

    https://alligator.io/vuejs/add-v-model-support/

  5. Python爬虫实战---抓取图书馆借阅信息

    Python爬虫实战---抓取图书馆借阅信息 原创作品,引用请表明出处:Python爬虫实战---抓取图书馆借阅信息 前段时间在图书馆借了很多书,借得多了就容易忘记每本书的应还日期,老是担心自己会违约 ...

  6. TableView的cell加载倒计时重用问题解决方案

    TableView的cell加载倒计时重用问题解决方案 效果 说明 1. 写过类似需求的朋友一定知道,TableView上面加载倒计时功能会遇到复杂的重用问题难以解决 2. 本人提供一种解决思路,高效 ...

  7. 使用CAReplicatorLayer [1]

    使用CAReplicatorLayer [1] 说明 https://developer.apple.com/library/ios/documentation/GraphicsImaging/Ref ...

  8. Linux学习---Linux用户审计简单版

    [root@localhost root]# vim /etc/profile # SHENJI history USER=`whoami` USER_IP=`who -u am i 2>/de ...

  9. python字典去重脚本

    #!/usr/bin/env python # encoding: utf-8 #字典去重小代码 import sys import os import platform try: pass exce ...

  10. ms17-010漏洞利用教程

    ms17-010 漏洞利用并拿下服务器教程 攻击环境: 攻击机win2003 ip:192.168.150.129 Window2003 Python环境及工具 攻击机kali: ip:192.168 ...