C++ strcat未冲洗
我在arduino有这个密码C++ strcat未冲洗,c++,arduino,strcat,C++,Arduino,Strcat,我在arduino有这个密码 void function(int x){ char* response="GET /postTEST.php?first="; char strx[2] = {0}; int num = x; sprintf(strx, "%d", num); original=response; strcat(response,strx); Serial.println(response); //memset
void function(int x){
char* response="GET /postTEST.php?first=";
char strx[2] = {0};
int num = x;
sprintf(strx, "%d", num);
original=response;
strcat(response,strx);
Serial.println(response);
//memset(response,'\0',80);
}
基本上,它是将一个整数连接到我的post字符串。不幸的是,它以某种方式增长并成为
GET/postest.php?first=0
GET/postest.php?first=01
GET/postest.php?first=012
当我增加的时候
为什么 不能修改字符串文字。字符串文字是常量 您必须将其声明为一个数组,该数组具有足够的空间来添加数字 你也会做一些不必要的步骤,我建议如下:
void function(int x)
{
char response[64];
sprintf(response, "GET /postTEST.php?first=%d", x);
Serial.println(response);
}
不能修改字符串文字。字符串文字是常量 您必须将其声明为一个数组,该数组具有足够的空间来添加数字 你也会做一些不必要的步骤,我建议如下:
void function(int x)
{
char response[64];
sprintf(response, "GET /postTEST.php?first=%d", x);
Serial.println(response);
}
传递的整数是一位数字吗?这就是您使用
strx[2]
为其分配的所有空间。传递的整数是一位数字吗?这就是您使用strx[2]
分配的所有空间。