我在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(response,'\0',80);
}基本上,它是将一个整数连接到我的post字符串。不幸的是,随着i的增加,它以某种方式增长并变成了GET /postTEST.php?first=0 GET /postTEST.php?first=01 GET /postTEST.php?first=012。
怎么会这样?
发布于 2013-02-24 20:31:44
不能修改字符串文字。字符串文字是常量。
您必须将其声明为一个数组,该数组具有足够的空间来添加数字。
你也做了一些不必要的步骤,我建议这样做:
void function(int x)
{
char response[64];
sprintf(response, "GET /postTEST.php?first=%d", x);
Serial.println(response);
}https://stackoverflow.com/questions/15051724
复制相似问题