在 C++ 中(在 Arduino 上)可以实现类似的功能吗?
#include "stdio.h"
String str = "foo";
int i[strLength()]; // <- define length of array in function
int strLength() {
return str.length();
}
int main(void) {
...
}
提前致谢!
最佳答案
如果您使用的是 C++,正确的解决方案是 std::vector。 您需要查看 std::vector 的文档,但这里是您的代码到 std::vector 的转换。
然后您可以像使用常规数组一样使用 std::vectors,使用“[]”运算符。
#include <cstdio>
#include <vector>
String str = "foo";
int strLength() { // Needs to come before the use of the function
return str.length();
}
std::vector<int> i(strLength() ); //Start at strLength
int main(void) {
...
}
关于c++ - 如何在 Arduino 的函数中定义全局数组的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8466554/