title: C - Malloc and memcpy (memory management)
body: <p>I'm a bit new to C and I'm having trouble understanding how memory works, especially in-built functions like <code>memcpy</code>.</p>
  
  <p>Here's a <code>struct</code> I'm using</p>
  
  <pre><code> struct data_t {
      int datasize;   
      void *data; 
   };
  </code></pre>
  
  <p>And here's an auxiliary function that I'm using it with:</p>
  
  <pre><code>struct data_t *data_create(int size)
  {
     struct data_t *dt=malloc(sizeof(struct data_t)+size);
     dt-&gt;datasize=size;   
     dt-&gt;data="1234567890a";
     return dt;
  }
  </code></pre>
  
  <p>Now in the <code>main</code> function I have no problem doing this:</p>
  
  <pre><code>struct data_t *data = data_create(1024);
  data-&gt;data="123456a";//just an example
  </code></pre>
  
  <p>But this throws a Seg Fault:</p>
  
  <pre><code>memcpy(data-&gt;data,"123456a",strlen("1234567890a")+1);
  </code></pre>
  
  <p>My question is why? And how do I avoid it?
  Please bear in mind I'm new to C so how C deals with memory is a bit new to me</p>
  
  <p>Thank you.</p>
  
  <p>Edit: It works! Thank you very much. Completly missed the data pointer. Now everything is working fine according to valgrind.</p>