Newbie C/C++ struct array pointing to other struct array elements -


i stumbled upon neat trick i've started using write binary files (flash) memory on arduino/esp8266 using library posted 1 of esp8266 forums. i've been trying number of ways expand upon it. i've been minifying , compressing web content files , compiling them in sketches on esp. script posted first uses output of unix command xxd -i write binary file array of hex. second part uses struct combine file details pointer array can reference code whenever server gets uri request matches entry in array.

what create second array of these things 'default' tools pre-compressed don't have go through every time and/or modify script builds header file time create new server sketch. compress , xxd stuff jquery.js, bootstrap.css , bootstrap.js (or more smaller counterparts backbone or barekit)

currently once file dumped hex, example:

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); 

the existing code added them @ once along struct definition:

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 file2 ...*/   },   {  /* details file3 ...*/   } }; 

building array of structs representing various files.

my questions amount noob questions regarding language syntax. can assume can use identical populated struct in place of inside curly brackets? example, if had second header file regularly used libraries, , jquery compressed in array called 'default_files' @ position 3, use &default_files[3] in place of { /* definitions stuffs */ }. such as:

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,   } }; 

(i'm guessing based on i've learned far needs & in front of it?)

i assume rather re-writing struct definition twice,i typedef , do:

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

is correct? appreciated. it's hard find details on syntax specific use cases in documentation covering basics. (i try it, i'm not conveniently in front of compiler @ moment nor have direct access codebase want work on later when might not have direct access net)

from understand, want create array of structs such contains both compound literals , items array, defined in header information. don't think possible - or @ least not in exact way suggest. i'll try , provide alternative though.

can assume can use identical populated struct in place of inside curly brackets?

no - you're mixing types. 'files' defined array of 'struct t_website'. code

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

won't compile mixing types. files defined array of struct t_websitefile, &default_files[1] pointer. c makes distinction between pointers , non-pointers. seperate types.

the obvious option can see want use pointers. allow define in header information.

struct t_websitefiles default_files[] = {    .... } struct t_websitefiles files[] = {    .... }  // array of pointers struct t_websitefiles *files_combined[] = {     &files[0],     &files[1],     &default_files[0],     // or whatever values want here     ... }  // example main, iterates through combined list //  of files int main(int argc, char* argv[]) {     int i;     int files_combined_len = sizeof(files_combined)/sizeof(struct t_websitefiles);      (i=0; i<files_combined_len; i++) {         printf("file %s\r\n", files_combined[i]->path);     }      return 0; } 

hope helps.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -