简体   繁体   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+'


In short, i have a function char getData() which returns char output[50] = "1: " + cmOne + " 2: " + cmTwo + " 3: " + cmThree + " 4: " + cmFour; 简而言之,我有一个函数char getData()返回char output[50] = "1: " + cmOne + " 2: " + cmTwo + " 3: " + cmThree + " 4: " + cmFour; where int cmOne, cmTwo, cmThree, cmFour . 其中int cmOne, cmTwo, cmThree, cmFour

In loop, i call: 在循环中,我称:

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

I know that i'm wrong with my data types, assigning etc but am abit off with how to do it best, any suggestions?? 我知道我的数据类型,分配等有误,但是对如何做到最好没有任何建议?

That's not possible, create a fixed size array, and pass it to the function as a pointer, and initialize it in the function 这是不可能的,创建一个固定大小的数组,并将其作为指针传递给函数,然后在函数中对其进行初始化

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

and of course if the array is bigger just char results[A_BIGGER_SIZE]; 当然,如果数组更大,只是char results[A_BIGGER_SIZE];

Suppose that get data just puts a string "ABC" in the result array it would look like 假设获取数据只是在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