CPP strcat函数使用
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中的空间是否充足。
参考文献
- https://blog.csdn.net/candyliuxj/article/details/6786718
- http://www.cplusplus.com/reference/cstring/strcat/
CPP strcat函数使用的更多相关文章
- C语言strcat()函数:连接字符串
头文件:#include <string.h> strcat() 函数用来连接字符串,其原型为: char *strcat(char *dest, const char *src); ...
- strcat函数的使用需要注意的问题
曾被这个函数困扰了好久,然后各种假设,验证:但是最后却发现这个函数并没有什么好讲的,原来的过错一切都源于忽略了“*dst去掉\0,然后加上*src,最后返回*dst”这句话的真正含义:给*dst分配的 ...
- strcat()函数常见问题
strcat(char *_Destination,const char *_Source)函数的功能是将后一个字符串粘贴到前一个字符串的末尾 原型 char *strcat(char *_Desti ...
- strcat函数造成的段错误(Segmentation fault)
转自:http://book.51cto.com/art/201311/419441.htm 3.21 strcat函数造成的段错误 代码示例 int main() { char dest[7]=& ...
- 【C语言】模拟实现库函数strcat函数
//模拟实现库函数strcat函数 #include <stdio.h> #include <string.h> #include <assert.h> char ...
- strcat函数的坑点
我们先看下面这样一段代码: #include <iostream> #include <stdlib.h> using namespace std; int main() { ...
- 编写一个程序实现strcat函数的功能
写自己的strcat函数------→mycat #include <stdio.h> #include <string.h> #define N 5 char *mycat( ...
- C语言中strcpy,strcmp,strlen,strcat函数原型
//strcat(dest,src)把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0' char *strcat(char * strDest, const char ...
- 由strcat函数引发的C语言中数组和指针问题的思考
问题一 首先,来看一下下面这段代码: #include <stdio.h> #include <string.h> int main() { char *str = " ...
随机推荐
- elixir 表单 map
键-值对 iex(13)> map = %{:a => 1, 2 => :b}%{2 => :b, :a => 1}iex(14)> map[:a]1iex(15) ...
- Keras & Theano 输出中间层结果
Keras & Theano get output of an intermediate layer 1.使用函数模型API,新建一个model,将输入和输出定义为原来的model的输入和想要 ...
- Android Studio 使用Intent实现页面的跳转(带参数)
不管是在APP,还是在网站中,页面之间的跳转都是很常见的,本文主要讲一下在APP中,如何通过Intent实现页面的跳转. 不带参数: 写在MainActivity页面的代码: Intent inten ...
- 润乾报表一个页面中的echarts地图与其他区块的联动
需求概述: DBD样式效果如下图所示,需要点击左侧地图中的地区,右侧的仪表盘,柱线图可以对应显示对应该地区的数据. 实现思路: 分别制作带有地图.仪表盘.柱线图的3张报表:将3张报表放到DBD中设置布 ...
- Vue 框架-10-搭建脚手架 CLI
Vue 框架-10-搭建脚手架 CLI + 批处理快捷启动 脚手架是通过 webpack 搭建的开发环境 使用 ES6 语法 打包和压缩 JS 为一个文件 项目文件在环境中,而不是浏览器 实现页面自动 ...
- Debian 版本历史
Production Releases Version Code name Release date End of life date 8.0 jessie April 25th 2015 7.0 ...
- Django 补充
在Django项目的外面操作这个Django内部的models: 当你创建Django项目的时候你在用的时候,你是在这个Django项目中使用的 那么你怎么在你的这个Django项目的外面使用这个D ...
- [控件] 心形加载的view
心形加载的view 效果: 素材图片: 源码: StarView.h 与 StarView.m // // StarView.h // Star // // Created by XianMingYo ...
- swift中变量的几种类型
swift中变量的几种类型 swift中变量分为 optional,non-optional 以及 implicitly unwrapped optional 这几种类型 var nullablePr ...
- word 排版用到双直线、波浪线、虚线 、直线、隔行线等技巧
在办公或毕业设计时,有时排版需要插入双直线.波浪线.虚线 .直线.隔行线等而烦恼, 今天小白与大家分享技巧如下: 感谢您的阅读,如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮.本文欢迎各位转载,但 ...