1

I am working with a wifi microcontroller that needs to serve up a large (20kb) static html page. Because individual buffers on the microcontroller only hold 1.4kb, it is necessary to break up the html into chunks and send the pieces one at a time.

What I have right now is about 100 string assignemnts that look like this:

char HTML_ID_96[] = "\
<p><a href=\"#t\">Return to top</a></p>\
<a id=\"id9\"/>\
<span class=\"s\">Firmware Version/Information</span>\
<span class=\"c i\" id=\"id9-h\" onclick=\"h(\'id9\');\">hide</span>&nbsp;\
<span class=\'c\' id=\"id9-s\" onclick=\"s(\'id9\');\">show</span>\
<table class=\"t i\" id=\"id9-table\"><tbody>\
";

I would like a way to impose an iterable sequence on all of the strings by sticking them in an array, but I am not sure how to package them.

I have tried:

char** all = [HTML_ID_1, ..., HTML_ID_99];
char* all[] = [HTML_ID_1, ..., HTML_ID_99];
char all[][] = [HTML_ID_1, ..., HTML_ID_99];

But none of them compile. Any references to how C handles arrays is a bonus.

Extension:

char const* HTML_ID_100 = "\
</form>\
</body>\
</html>\
";

char const* all[] = {HTML_ID_100};

Is not compiling. I'm using gcc 3.4.4. Two errors are reported: "initializer element is not constant" and "(near initialization for 'all[0]')". Both occurring on the last line shown.

waldol1
  • 1,841
  • 2
  • 18
  • 22
  • 8
    Initializers use `{}`, not `[]`. – chirlu May 23 '13 at 15:00
  • Useful hint: The C pre-processor performs automatic concatenation on string literals. So you can write your code as `char HTML_ID_96[] =` (new line) `"

    Return to top

    "` (new line) `""` (new line). And so on. No need for the icky backslash.
    – Lundin May 23 '13 at 15:35
  • For the extension, see http://stackoverflow.com/questions/3025050/ – do you need the HTML_ID_* identifiers, or could you say `char *all [] = { "String 1", "String 2", ... };` directly? – chirlu May 23 '13 at 16:09
  • @chirlu Yes, I do need all of those identifier. Depending on the type of micro controller, I need different sequences of strings. I plan to construct at least two sequences utilizing mostly the same strings. – waldol1 May 23 '13 at 16:54
  • Then either set the array programmatically (`all[0] = HTML_ID_1; all[1] = HTML_ID_2; ...`), or `#define` the strings (but then you may need a clever linker that merges identical string literals). – chirlu May 23 '13 at 17:24

3 Answers3

8

Use braces for array initialization.

char* all[] = { HTML_ID_1, ..., HTML_ID_99 };
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
4

An array of pointers to string:

char* all[] = {HTML_ID_1, ..., HTML_ID_99};

Note that you may want to terminate the array with a NULL pointer depending on how you're going to iterate over the array:

char* all[] = {HTML_ID_1, ..., HTML_ID_99,NULL};

Also, if the strings aren't going to be modified, you can save some data space by declaring them as simple pointers to the literal strings instead of arrays of char that are initialized by the literal:

char const* HTML_ID_96 = "\
<p><a href=\"#t\">Return to top</a></p>\
<a id=\"id9\"/>\
<span class=\"s\">Firmware Version/Information</span>\
<span class=\"c i\" id=\"id9-h\" onclick=\"h(\'id9\');\">hide</span>&nbsp;\
<span class=\'c\' id=\"id9-s\" onclick=\"s(\'id9\');\">show</span>\
<table class=\"t i\" id=\"id9-table\"><tbody>\
";
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • Do the line terminators (trailing '\' on a line) turn into newline characters when compiled? – waldol1 May 23 '13 at 15:14
  • @waldol1: my mistake - there would be no difference in the resulting string, so I removed that last bit. – Michael Burr May 23 '13 at 15:17
  • Okay, now I'm trying a declaration like you suggested `char const* id = "...";` How do I declare an array of those types? My intuition says that it should be `char const* all[] = {...};` but the compiler disagrees. – waldol1 May 23 '13 at 15:17
  • @waldol1: that looks right to me (and works here). What is the exact error message and what compiler are you using? – Michael Burr May 23 '13 at 15:20
2

You're on the right track but you need to use curly braces for a static array declaration. This should work:

char* all[] = {HTML_ID_1, ..., HTML_ID_99};

An example

I would think about what you are doing and if there is a better way to do it though. E.g. if it is a micro, can you make a large array in program memory (i.e the NVRAM) and read from it serially?

  • That might work, but different versions of the controller need different subsections of the html, so I need to be able to splice them dynamically. – waldol1 May 23 '13 at 15:06