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 = " ...
随机推荐
- iis配置asp.net常见问题解决方案
很多朋友在用IIS6架网站的时候遇到不少问题,而这些问题有些在过去的IIS5里面就遇到过,有些是新出来的,俺忙活了一下午,做 了很多次试验,结合以前的排错经验,做出了这个总结,希望能给大家帮上忙:) ...
- MySql 缓存查询原理与缓存监控 和 索引监控
MySql缓存查询原理与缓存监控 And 索引监控 by:授客 QQ:1033553122 查询缓存 1.查询缓存操作原理 mysql执行查询语句之前,把查询语句同查询缓存中的语句进行比较,且是按字节 ...
- Pwn Heap With Tcache
Pwn Heap With Tcache 前言 glibc 2.26 开始引入了 tcache , 相关的 commit 可以看 这里 .加入 tcache 对性能有比较大的提升,不过由于 tcach ...
- elipse安装php
在用eclipse作为PHP的开发IDE工具时,如果下载的Eclipse不带有PHP功能,则需要我们自己来给Eclipse升级.不过也可以下载eclipseForPHP 在Eclipse的help菜单 ...
- leetcode题解之分解字符串域名
1.题目描述 A website domain like "discuss.leetcode.com" consists of various subdomains. At the ...
- 记开发个人图书收藏清单小程序开发(七)DB设计
前面的书房初始化的前端信息已经完善,所以现在开始实现DB的Script部分. 新增Action:Shelf_Init.sql svc.sql CREATE SCHEMA [svc] AUTHORIZA ...
- Mysqlbinlog工具及导出数据并转换编码导入
2014 - binlog是通过记录二进制文件方式来备份数据,然后在从二进制文件将数据恢复到某一时段或某一操作点. 1.使用mysqlbinlog工具来恢复 Mysqlbinlog日志如何开启? 在m ...
- [翻译] CoreImage-with-EAGLContext
CoreImage-with-EAGLContext https://github.com/anaglik/CoreImage-with-EAGLContext Simple example of d ...
- [翻译] RDVTabBarController
RDVTabBarController https://github.com/robbdimitrov/RDVTabBarController 效果: Supports iPad and iPhone ...
- nodepad++添加新主题
https://www.cnblogs.com/d0main/p/6915460.html