title: Get and set values in a dynamically allocated array of structures
body: <p>I am a beginner in programming C and need help with getting and setting  values when I create a dynamic array of structures.</p>
  
  <p>I have this struct for example:</p>
  
  <pre><code>struct opt{
      int size;
      Element* elements;
      CopyFunction copyElement;
  };
  typedef struct opt * Popt;
  </code></pre>
  
  <p>I want to use dynamic allocation and use <code>malloc</code> to allocate memory for 5 structs of type <code>struct opt</code>; I used this line:</p>
  
  <pre><code>Popt opt_t = (Popt)malloc(5*sizeof(struct opt));
  </code></pre>
  
  <p>I want to get and set values in these structures. If I think about that like OOP, I would like to get the values like this:</p>
  
  <blockquote>
    <p>opt_t[2].size, opt_t[3].elements, opt_t[3].copyElement</p>
  </blockquote>
  
  <p>How can I do it with C? I tried to use the following lines and its looking bad (my memory filled with garbage).</p>
  
  <pre><code>*((&amp;(opt_t-&gt;size)+(2*sizeof(struct opt))))
  *((&amp;(opt_t-&gt;elements)+(3*sizeof(struct opt))))
  *((&amp;(opt_t-&gt;copyElement)+(3*sizeof(struct opt))))
  </code></pre>