strcat not flushing

strcat not flushing

本文关键字:flushing not strcat      更新时间:2023-10-16

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

基本上,它是将一个整数加入到我的帖子字符串中。不幸的是,它以某种方式成长获取/postTEST.php?第一个=0获取/postTEST.php?第一个=01获取/postTEST.php?第一个=012随着i的增加。

为什么?

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

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

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

void function(int x)
{
    char response[64];
    sprintf(response, "GET /postTEST.php?first=%d", x);
    Serial.println(response);
}