title: Allocate memory for buffer (of type struct) in C
body: <p>I'm writing a C program with the following structs:</p>
  
  <pre><code>struct packet_info{
      char *ip_src;
      char *ip_dst; 
      u_short th_sport;
      u_short th_dport; 
      u_int   th_seq;
  };
  
  struct key_value{
      struct packet_info key;
      long unsigned int value;
  };
  </code></pre>
  
  <p>I want to create a buffer of the type <code>key_value</code> with 1000 items (MAX_SIZE). However, I don't know how to allocate memory for it.</p>
  
  <p>I was doing it like this:</p>
  
  <pre><code>struct key_value *buffer = malloc(MAX_SIZE * sizeof(struct key_value))
  </code></pre>
  
  <p>However, after reading the following question/answer -  <a href="https://stackoverflow.com/questions/14768230/malloc-for-struct-and-pointer-in-c">malloc for struct and pointer in C</a> - I came to the conclusion that I would also need to allocate space for the <code>char* ip_src</code> and <code>char* ip_dst</code>.</p>
  
  <p>What is, after all, the right way to allocate memory for the buffer?</p>