2013-02-24 48 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); 
}