c++ - 如何在 Arduino 的函数中定义全局数组的长度?

标签 c++ arrays global arduino

在 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/

相关文章:

c++ - 每次创建 C++ 文件时,VsCode 都会创建一个 .exe 文件

arrays - 将列表加入一个字符串

r - 从一个函数中向.GlobalEnv分配多个对象

c++ - 递归函数? [初学者]

c++ - 为什么在 Gnu gcc/g++ 中为三字母序列解析字符串文字?

c++ - 使用鼠标从视频上的矩形设置 ROI

java - 为什么我的 native C++ 代码在 Android 上的运行速度比 Java 慢得多?

android - ArrayAdapter 错误

android - 尝试将 mutableListOf<String> 存储到 SharedPreferences 但无法设置默认值

python - 当你在类/函数内外都声明了同名变量时,你怎么知道将使用变量的哪个值?