0

我在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);
}

基本上,它是将一个整数加入我的帖子字符串。不幸的是,当我增加 i 时,它会以某种方式增长并变成 GET /postTEST.php?first=0 GET /postTEST.php?first=01 GET /postTEST.php?first=012。

怎么来的?

4

1 回答 1

3

您不能修改字符串文字。字符串文字是常量。

您必须将其声明为具有足够空间的数组来添加数字。

你也做了一些不必要的步骤,我建议这样的事情:

void function(int x)
{
    char response[64];

    sprintf(response, "GET /postTEST.php?first=%d", x);

    Serial.println(response);
}
于 2013-02-24T12:31:44.337 回答