title: Initialize pointer to const pointer
body: <p>Is what am I trying to do in C possible?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct foo{
      int const * const a;    // constPtrToConst is a constant (pointer)
                              // as is *constPtrToConst (value)
  };
  
  struct faa{
      int b;
  };
  
  int main(void){
      struct foo *x = (struct foo*)malloc(sizeof(struct foo));
      struct faa *y = (struct faa*)malloc(sizeof(struct faa));
  
      x-&gt;a = &amp;(y-&gt;b);     // error: assignment of read-only member ‘a’ [that's ok] 
                          // (I)
      x-&gt;a++;    // It should not do this either...
  
      printf("%d,\t%p\n", *(x-&gt;a), x-&gt;a);    // (II)
      free(x);
      free(y);
  }
  </code></pre>
  
  <p>How can I initialize (I) and could I get this (II)?</p>
  
  <p>Sorry is not assign is initialize with that pointer.</p>
  
  <p>This is what I want to get but dynamically.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  struct foo{
      int const * const a;
  };
  
  int main(void){
      int b = 5;
      struct foo x = {
          .a = &amp;b
      };
      printf("%d\n", *(x.a));
  }
  </code></pre>
  
  <h1>This is how I solve it.</h1>
  
  <h3>I don't know if is the best choice.</h3>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  struct foo{
      int const * const a;
  };
  
  struct foo* newfoo(int *var){
      struct foo *tempD = malloc(sizeof(struct foo));
      struct foo tempS ={
          .a = var
      };
      memcpy(tempD, &amp;tempS, sizeof(struct foo));
  
      return tempD;
  }
  
  int main(void){
      int b = 5;
  
      struct foo *z = newfoo(&amp;b);
  
      printf("%d,\t%p\n", *(z-&gt;a), z-&gt;a);
      // this is equivalent to printf("%d,\t%p\n", b, &amp;b);
  
      free(z);
  }
  </code></pre>