title: Assignment of char array to struct with type casting
body: <p>I'm working on my C program code to allocate memory without using <code>malloc()</code>. My friend was using a linked list to manage memory, and I do it on this example but I don't know how this type of assignment works in function <code>memory_init()</code>.</p>
  
  <p>Here's the main of the example test:</p>
  
  <pre><code>#include &lt;string.h&gt; 
  int main() {   
    char region[50];   
    memory_init(region, 50);   
    char* pointer = (char*) memory_alloc(10);    
    if (pointer)     memset(pointer, 0, 10);   
    if (pointer)     memory_free(pointer);   
    return 0;
  } 
  </code></pre>
  
  <p>and there's function <code>memory_init()</code>:</p>
  
  <pre><code>void memory_init(void *ptr, unsigned size) {    
  
      struct metadata *first;
      first=(struct metadata*)ptr;
      first-&gt;next=NULL;
      first-&gt;prev=NULL;
      if (size &gt; sizeof(struct metadata)) first-&gt;value=(size - sizeof(struct metadata));
      else first-&gt;value=0;
      first-&gt;boo=-1;
      zac=first;
  }
  </code></pre>
  
  <p>and there's also used struct:</p>
  
  <pre><code>struct metadata{        
      short boo;      // if -1 free, if 1 memory used
      unsigned value; //size of memory block
      struct metadata *next; 
      struct metadata *prev;  
  };
  </code></pre>
  
  <p>So I don't know how this part of the code works:</p>
  
  <pre><code>first=(struct metadata*)ptr;
  </code></pre>
  
  <blockquote>
    <p>At what I see it it takes a char array sent to the function and retypes it to
    a struct and assigns it, but I don't know how assignment like that works?
    Does it assign a memory address or what because I don't take how I can retype
    char to a struct. Basically I would like to know what happens there.</p>
  </blockquote>