簡體   English   中英

arduino,函數返回char數組

[英]arduino, function to return char array

_10_11.ino: In function 'void loop()':
_10_11:73: error: initializer fails to determine size of 'results'
_10_11.ino: In function 'char getData()':
_10_11:160: error: invalid operands of types 'const char*' and 'const char [5]' to binary 'operator+'


簡而言之,我有一個函數char getData()返回char output[50] = "1: " + cmOne + " 2: " + cmTwo + " 3: " + cmThree + " 4: " + cmFour; 其中int cmOne, cmTwo, cmThree, cmFour

在循環中,我稱:

char results[] = getData();

    client.println("1: %i", results[0]);
    client.println("2: %i", results[1]);
    client.println("3: %i", results[2]);
    client.println("4: %i", results[3]);

我知道我的數據類型,分配等有誤,但是對如何做到最好沒有任何建議?

這是不可能的,創建一個固定大小的數組,並將其作為指針傳遞給函數,然后在函數中對其進行初始化

char results[4];

getData(results); /* the getData function should take a 'char *' paramenter */

client.println("1: %i", results[0]);
client.println("2: %i", results[1]);
client.println("3: %i", results[2]);
client.println("4: %i", results[3]);

當然,如果數組更大,只是char results[A_BIGGER_SIZE];

假設獲取數據只是在result數組中放入一個字符串"ABC" ,看起來像

void getData(char *dest)
{
    dest[0] = 'A';
    dest[1] = 'B';
    dest[2] = 'C';
    dest[3] = '\0';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM