能否有人告诉我有什么不对的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的不工作追加字符数组
发布评论