c++ - arduino,函数返回字符数组

标签 c++ c arduino

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

假设获取数据只是将字符串 "ABC" 放入 result 数组中,它看起来像

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

关于c++ - arduino,函数返回字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27888152/

相关文章:

c++ - 如何在esp32中操作sd模块?

c - 在arduino中什么是SREG?

c++ - 如何让月份停止显示 1.00、2.00 等

c++ - OpenSSL有没有 'Read'数据的事件回调函数?

c - 如何使用单词作为哈希中的键

c - 如何让我的代码进行正确的计算?

将 NULL 强制转换为 C 中的结构指针?

无法从 arduino 发送 RS-232 命令

c++ - 有什么方法可以控制 C++ 中结构成员(包括位字段)之间的填充?

c++ - 为什么Dev C++编译后PE文件中多了这么多节?