使用strcat的不工作追加字符数组

编程入门 行业动态 更新时间:2024-10-28 21:27:34
本文介绍了使用strcat的不工作追加字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

能否有人告诉我有什么不对的code ???

字符的短信[] =GR8;strcat的(短信,&安培; :));

解决方案

短信是大小的数组 4 1 。而你更追加字符文字,这是怎么回事阵列之外,作为数组可以在最大 4 这已经是由占据容纳的字符G,R,8,\\ 0 。

1。顺便说一句,到底为什么4?答:因为有在最后一个空字符!

如果你提到数组的大小,如下图所示,那么你的code是有效的和明确的。

字符的短信[10] =GR8; //确保数组的大小为10                       //所以可以几个字符后追加。strcat的(短信,&安培; :));

但随后C ++为您提供更好的解决方案:使用的std ::字符串为:

的#include<串GT; //必须标准::字符串短信=GR8;短信+ =&放大器; :) //字符串连接 - 轻松,可爱!

Can some one tell me what's wrong with this code???

char sms[] = "gr8"; strcat (sms, " & :)");

解决方案

sms is an array of size 4 1. And you're appending more char literals, which is going outside of the array, as the array can accommodate at max 4 chars which is already occupied by g, r, 8, \0.

1. By the way, why exactly 4? Answer : Because that there is a null character at the end!

If you mention the size of array as shown below, then your code is valid and well-defined.

char sms[10] = "gr8"; //ensure that size of the array is 10 //so it can be appended few chars later. strcat (sms, " & :)");

But then C++ provides you better solution: use std::string as:

#include <string> //must std::string sms = "gr8"; sms += " & :)"; //string concatenation - easy and cute!

更多推荐

使用strcat的不工作追加字符数组

本文发布于:2023-07-29 06:41:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1238198.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符   工作   strcat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!