c++ - 指向其他结构数组元素的新手 C/C++ 结构数组

标签 c++ struct arduino arduino-esp8266

我偶然发现了一个巧妙的技巧,我开始使用这个技巧使用某人发布到 esp8266 论坛之一的库将二进制文件写入 arduino/esp8266 上的(闪存)内存。我一直在尝试多种方法来扩展它。最近我一直在缩小和压缩我的网络内容文件,并在我的 ESP 上用草图编译它们。 他发布的脚本首先使用 unix 命令 xxd -i 的输出将二进制文件写入十六进制数组。第二部分使用结构将文件详细信息与指向数组的指针组合在一起,只要服务器收到与数组中的条目匹配的 uri 请求,您就可以从代码中引用该数组。

我想做的是使用已经预压缩的“默认”工具创建第二个这些东西的数组,这样我就不必每次都经历它和/或修改我的脚本来构建头文件是时候创建一个新的服务器草图了。基本上是压缩和 xxd 的东西,比如 jquery.js、bootstrap.css 和 bootstrap.js(或者更常见的是它们较小的对应物,比如 backbone 或 barekit)

目前一旦一个文件转储为十六进制,例如:

FLASH_ARRAY(uint8_t, __js__simple_js,
  0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x4b, 0x2b,
  0xcd, 0x4b, 0x2e, 0xc9, 0xcc, 0xcf, 0x53, 0xc8, 0xad, 0xf4, 0xcf, 0xf3,
  0xc9, 0x4f, 0x4c, 0xd1, 0xd0, 0xac, 0x4e, 0xcc, 0x49, 0x2d, 0x2a, 0xd1,
  0x50, 0x0a, 0xc9, 0xc8, 0x2c, 0x56, 0x00, 0xa2, 0xc4, 0x3c, 0x85, 0xfc,
  0xbc, 0x1c, 0xa0, 0x94, 0x42, 0x6e, 0x6a, 0x71, 0x71, 0x62, 0x7a, 0xaa,
  0x92, 0xa6, 0x75, 0x51, 0x6a, 0x49, 0x69, 0x51, 0x9e, 0x42, 0x49, 0x51,
  0x69, 0x6a, 0x2d, 0x00, 0x16, 0xa6, 0x25, 0xe5, 0x43, 0x00, 0x00, 0x00);

现有代码将它们与结构定义一起添加:

struct t_websitefiles {
  const char* path;
  const char* mime;
  const unsigned int len;
  const char* enc;
  const _FLASH_ARRAY<uint8_t>* content;
} files[] = {
{
    .path = "/js/simple.js",
    .mime = "application/javascript",
    .len = 84,
    .enc = "gzip",
    .content = &__js__simple_js,
  },
  {
 /* details for file2 ...*/
  },
  {
 /* details for file3 ...*/
  }
};

构建代表各种文件的结构数组。

我的问题相当于关于语言语法的菜鸟问题。我可以假设我可以使用相同的填充结构来代替大括号内的内容吗?例如,如果我有第二个头文件和我经常使用的库,并且 jquery 被压缩在位置 3 的名为“default_files”的数组中,我可以使用类似 &default_files[3] 的东西代替 {/* definitions stuffs */}。如:

struct t_websitefiles {
  const char* path;
  const char* mime;
  const unsigned int len;
  const char* enc;
  const _FLASH_ARRAY<uint8_t>* content;
} files[] = {
{
    .path = "/js/simple.js",
    .mime = "application/javascript",
    .len = 84,
    .enc = "gzip",
    .content = &__js__simple_js,
  },
  &default_files[1],
  &default_files[3],
{
    .path = "/text/readme.txt",
    .mime = "text/text",
    .len = 112,
    .enc = "",
    .content = &__text__readme_txt,
  }
};

(根据我目前所学的知识,我猜它前面需要 & 吗?)

我还假设不是重写结构定义两次,我可以把它作为一个 typedef 然后做:

t_websitefiles files[] = { {/*definitions*/},{ /*stuffs*/ } };

这样对吗?任何帮助表示赞赏。有时很难在涵盖基础知识的文档中找到有关特定用例语法的详细信息。 (我只是想尝试一下,但目前我不方便在编译器前,也不能直接访问我的代码库,但想稍后在我可能无法直接访问网络时处理它)

最佳答案

据我了解,您想要创建一个结构数组,其中包含复合文字和来自另一个数组的项目,所有这些都在 header 信息中定义。 我不认为这是可能的 - 或者至少不是你建议的那样。不过,我会尝试提供替代方案。

Can I assume that I can use an identical populated struct in the place of what is inside the curly brackets?

不 - 你在混合你的类型。 "file"被定义为“struct t_website”的数组。 代码

struct t_websitefiles files[] = {
    ...
    &default_files[1],
    ...
}

不会编译,因为你正在混合你的类型。 files 被定义为一个struct t_websitefile 的数组,但是&default_files[1] 是一个指针。 C 区分指针和非指针。它们是不同的类型。

我可以看到做你想做的事情的明显选择是使用指针。这将允许您在标题信息中定义所有内容。

struct t_websitefiles default_files[] = {
   ....
}
struct t_websitefiles files[] = {
   ....
}

// An array of pointers
struct t_websitefiles *files_combined[] = {
    &files[0],
    &files[1],
    &default_files[0],
    // Or whatever values you want here
    ...
}

// Example main, just iterates through the combined list
//  of files
int main(int argc, char* argv[]) {
    int i;
    int files_combined_len = sizeof(files_combined)/sizeof(struct t_websitefiles);

    for (i=0; i<files_combined_len; i++) {
        printf("File %s\r\n", files_combined[i]->path);
    }

    return 0;
}

希望这对您有所帮助。

关于c++ - 指向其他结构数组元素的新手 C/C++ 结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45376629/

相关文章:

arduino - SIM900 GSM/GPRS 未获得正确的 AT+CREG?回答

c - 有没有办法让我的补充代码更加高效并允许一次多个输出?

c++ - 在 C++/Arduino 的结构中打包位

c++ - 如何在没有 for 循环的情况下直接找到指向映射中两个不同键的任意两个迭代器之间的元素数?

c++ - 防止返回私有(private)内部类实例

c++ - 遇到内部编译错误怎么办?

c# - 从 C# 中的 C++ 函数返回数组和结构

python - 在Python结构中填充字节

matlab - 是否有不强制字段顺序的 Matlab 结构变体?

c++ - 为什么我的本地 dll 在设置 _NT_SYMBOL_PATH 后永远加载?