2013-02-24 25 views
0

我在Arduino的这段代码strcat的不冲水

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

基本上,这是加入到我的帖子字符串的整数。不幸的是,它不知怎么成长,并且变成了 GET /postTEST.php?first=0 GET /postTEST.php?first=01 GET /postTEST.php?first=012 随着我增加i。

怎么回事?

+0

是传递的整数一个数字?这就是你用'strx [2]'分配空间的全部内容。 – 2013-02-24 12:30:42

回答

3

您不能修改字符串文字。字符串文字是不变的。

你必须声明它为一个有足够空间添加数字的数组。

你也做一些不必要的步骤,我认为是这样的:

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

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

    Serial.println(response); 
}