title: C++ , create structure with an optional value
body: <p>I need to create structure with an optional value :</p>
  <pre><code>typedef struct pkt_header{
      unsigned short Packet_Type;
      unsigned short Unprotected_Payload_Length;
      unsigned short Protected_Payload_Length; // optional (present/not present)
      unsigned short Version;   
  } PKT_HEADER;
  </code></pre>
  <p>How can i sometimes use <code>pkt_header-&gt;Protected_Payload_Length</code> and sometimes not use this value in a <code>struct</code> when the field is not present ?
  My first idea is to declare <code>unsigned char * Protected_Payload_Length</code> and pass <code>NULL</code> when i not use the field and use the <code>unsigned char*</code> for store my <code>unsigned short</code> value.</p>
  <pre><code>typedef struct pkt_header{
      unsigned short Packet_Type;
      unsigned short Unprotected_Payload_Length;
      unsigned char * Protected_Payload_Length; // optional
      unsigned short Version;   
  } PKT_HEADER;
  </code></pre>
  <p>I prepare my packet like this (and send this):</p>
  <pre><code>PKT_HEADER header;
  header.Packet_Type                = 0x0001;
  header.Unprotected_Payload_Length = 0x0b00;
  header.Protected_Payload_Length   = NULL; 
  header.Version                    = 0x0000;
  </code></pre>
  <p>I receive response and do this :</p>
  <pre><code>PKT_HEADER * header= (PKT_HEADER*)recvbuf;
  printf(&quot;Packet_Type                : %04x\n&quot;, header-&gt;Packet_Type);
  printf(&quot;Unprotected_Payload_Length : %04x\n&quot;, header-&gt;Unprotected_Payload_Length);
  printf(&quot;Version                    : %04x\n&quot;, header-&gt;Version);
  </code></pre>
  <p>But in this case, if i understand correctly, <code>unsigned char * Protected_Payload_Length</code> contain a pointer with a length of 4 bytes then <code>header-&gt;Protected_Payload_Length</code> contain <code>4 bytes</code> but i need <code>0 byte</code> because the value/field is not present in this precise case.</p>
  <p>Do I have to declare an appropriate structure in the data format or is there some other way to play with the structures?</p>
  <p>Thanks for your help.</p>