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 に答える