title: initialize struct contain references to structs
body: <p>Is it possible to have a struct containing references to structs. And how are these initialized? See short example below.</p>
  
  <p>Thanks</p>
  
  <pre><code>typedef struct {
    int a;
  }typeInner1;
  
  
  typedef struct {
    int b;
  }typeInner2;
  
  
  typedef struct {
    typeInner1 &amp;one;
    typeInner2 &amp;two;
  }typeOuter;
  
  void fun2(typeOuter *p){
    p-&gt;one.a =2;
    p-&gt;two.b =3;
  }
  
  
  void fun(typeInner1 &amp;arg1,typeInner2 &amp;arg2){
    typeOuter *ptr = new typeOuter;//&lt;-- how to write initializer
    fun2(ptr);
  }
  
  
  int main(){
    typeInner1 arg1;
    typeInner2 arg2;
    fun(arg1,arg2);
  
    //now arg1.a should be 2 and arg2.a=3
  }
  </code></pre>
  
  <p>Ok thanks for all the input. I also had to modify the typedef of the typeOuter to make it work. Full working code below for other people finding this post.</p>
  
  <pre><code>#include &lt;cstdio&gt;
  typedef struct {
    int a;
  }typeInner1;
  
  
  typedef struct {
    int b;
  }typeInner2;
  
  
  typedef struct typeOuter_t {
    typeInner1 &amp;one;
    typeInner2 &amp;two;
    typeOuter_t(typeInner1 &amp;a1, typeInner2 &amp;a2) : one(a1), two(a2) {}
  }typeOuter;
  
  void fun2(typeOuter *p){
    p-&gt;one.a =2;
    p-&gt;two.b =3;
  }
  
  
  void fun(typeInner1 &amp;arg1,typeInner2 &amp;arg2){
    typeOuter *ptr = new typeOuter(arg1,arg2);
    fun2(ptr);
  }
  
  
  int main(){
    typeInner1 arg1;
    typeInner2 arg2;
    fun(arg1,arg2);
  
    //now arg1.a shoule be 1 and arg2.a=3
    fprintf(stderr,"arg1=%d arg2=%d\n",arg1.a,arg2.b);
  }
  </code></pre>