Arduino,返回字符数组的函数
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];
假设获取数据只是将字符串"ABC"
放入result
数组中,如下所示
void getData(char *dest)
{
dest[0] = 'A';
dest[1] = 'B';
dest[2] = 'C';
dest[3] = ' ';
}
相关文章:
- 第 i 个元素返回 i 的函数数组
- 使用嵌套函数数组是个好主意吗?
- 正确实现类函数数组
- 试图引用已删除函数数组的相等运算符
- 如何在静态函数中使用成员函数数组
- 无法创建函数数组
- 构造函数数组
- 使用开关或定义函数数组 - 更快
- 编写一个函数,该函数将覆盖主函数数组中的值
- 可以像'funs[1]();'一样调用的函数数组
- std::AVX内部函数数组
- 如果数组大小未知,如何将函数数组作为参数传递
- 从函数数组中获取值
- 使用元编程在编译时初始化函数数组
- 将C函数数组转换为C++函数指针数组
- 复制构造函数 [数组] C++
- 添加函数数组并随机选择其中一个
- 指针到私有成员函数数组
- 可以单独声明函数数组的元素
- (c++)将main中的随机值保存到函数数组中