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. Visualizing MNIST with t-SNE, MDS, Sammon’s Mapping and Nearest neighbor graph

    MNIST 可视化 Visualizing MNIST: An Exploration of Dimensionality Reduction At some fundamental level, n ...

  2. 润乾在jetty应用服务器下的JNDI配置一

     一. 此处绑定的数据源是以 DBCP 为实现.首先必须将数据库驱动(这里用了MYSQL数据库)和DBCP所需要的 Jar 包复制到 Jetty 根目录的 lib 目录下.DBCP主要需要以下3个 ...

  3. Vue 框架-09-初识组件的应用

    Vue 框架-09-初识组件的应用 今天的第一个小实例,初步使用组件: 在 app.js 中定义模板组件,在 html 文件中使用自定义标签来显示 js 文件中定义的 html 代码块 比如说,下面定 ...

  4. python学习手册中的一些易忘的点(前三部分)

    1.ubuntu下让python脚本可直接运行: test.py文件(后缀可省)#!/usr/bin/pythonprint('wwwww') sudo chmod +x ./test.py (sud ...

  5. 国内一元钱 正常搭建android开发环境

    如果你人在gfw之外,那么此篇文章对你来说毫无用处,请自动略过.. 笔者自android出来之后,就一直想尝试一下.可惜,几年来一直未能够定下身心来研究尝试.而所做的工作也与android没有关系,所 ...

  6. Spring常用注解简单汇总

    使用注解之前要开启自动扫描功能,其中base-package为需要扫描的包(含子包). <context:component-scan base-package="cn.test&qu ...

  7. CompletionService和ExecutorCompletionService

    CompletionService用于提交一组Callable任务,其take方法返回已完成的一个Callable任务对应的Future对象.   如果你向Executor提交了一个批处理任务,并且希 ...

  8. 使用CoreData [2]

    使用CoreData [2] 此篇讲解CoreData处理关系型数据. 1. 先创建出Student于Teacher的实体. 2. 确定关系,并修改描述 3. 创建对象,并查看一下关系(Teacher ...

  9. Python入门-模块2(random模块、os模块)

    >>> random.randomrange(1,10) #返回1-10之间的一个随机数,不包括10 >>> random.randint(1,10) #返回1-1 ...

  10. dev richEditControl控件 设置文字 字体 大小

    Document doc = NoticeContentRichEditControl.Document; doc.BeginUpdate(); doc.Text = "需要设置格式的文字& ...