Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>#include &lt;stdio.h&gt; typedef struct { int length; char items[]; /* 1 */ } chararray_t; typedef struct { long hash; chararray_t chars; /* 2 */ } string_t; /*static string_t s1 = { 617862378, { 5, { 'H', 'e', 'l', 'l', 'o' } } // 3 };*/ static string_t s1 = { 617862378, {6,"Hello"} /* 3 */ }; int main() { printf("%d %d %s\n",s1.hash,s1.chars.length,s1.chars.items); return 0; } </code></pre> <p>Add 1 for the null character, et voila! :)</p> <p>Edit, Also works for 2 levels of nesting (GCC 4.8.0)</p> <pre><code>#include &lt;stdio.h&gt; typedef struct { int length; char items[]; /* 1 */ } chararray_t; typedef struct { long hash; chararray_t chars; /* 2 */ } string_t; typedef struct { long number; string_t arr; }experiment_t; static experiment_t s1 = { 617862378, {786,{6,"Hello"}} /* 3 */ }; int main() { printf("%d %d %d %s\n",s1.number,s1.arr.hash,s1.arr.chars.length,s1.arr.chars.items); return 0; } </code></pre> <p>----------EDIT 2------------------ Found a way around the limitation <a href="https://stackoverflow.com/questions/1558025/c-initialize-array-within-structure">C initialize array within structure</a></p> <p>Final code::</p> <pre><code>#include &lt;stdio.h&gt; typedef struct { int length; int *items; /* 1 */ } intarray_t; typedef struct { long hash; intarray_t chars; /* 2 */ int dummy[2]; } string_t; /*string_t s1 = { 617862378, { 6, {1,2,3,4,5,6} }, { 0,0 } };*/ string_t s1 = {617862378,{},{0,0}}; int main() { int i=0; intarray_t t1 = {.length = 6, .items = (int[6]){1,2,3,4,5,6}}; s1.chars = t1; printf("%d %d\n",s1.hash,s1.chars.length); while(i&lt;s1.chars.length) { printf("%d",s1.chars.items[i]); i++; } putchar('\n'); return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload