title: Find the no. of elements in an array of structs?
body: <p>It's weird because I have done it before but it just is not working. I have a structure <code>xmp_frame</code> like this.</p>
  
  <pre><code>typedef struct {
      short int attr_dtype;
      short int attr_code;
      short int attr_len;
      const char *attr;
  }xmp_frame;
  </code></pre>
  
  <p>Now I create an array of <code>xmp_frame</code> and use them like:</p>
  
  <pre><code>xmp_frame frame_array[]={
      {1,2,strlen("Hello there"),"Hello there"},
      {1,3,strlen("This is not working"),"This is not working"},
      {0,3,strlen("But why??"),"But why??"}
  };
  </code></pre>
  
  <p>Now I have a routine which basically writes the <code>frame_array</code> in to file:</p>
  
  <pre><code>short int write_frames(xmp_frame frame_array[],FILE *outfp){
  
  }
  </code></pre>
  
  <p>Before I write the <code>frame_array</code> I need to get the no. of elements in <code>frame_array[]</code> for doing some processing. So this is how we do it (generally):</p>
  
  <pre><code>short int write_frames(xmp_frame frame_array[],FILE *outfp) {
      short intnum_frames=sizeof(frame_array) / sizeof(frame_array[0]);
     /*But i get the value of num_frames as 0. I will print the outout of some debugging.*/ 
  
      fprintf(stderr,"\n Size of frame_array : %lu",sizeof(frame_array));  //prints 8
          fprintf(stderr,"\n Size of frame_array[0] : %lu",sizeof(frame_array[0])); //prints 16
         fprintf(stderr,"\n So num. of frames to write  : %d",  (sizeof(frame_array))/(sizeof(frame_array[0]))); //prints 0 
  }
  </code></pre>
  
  <p>Of course if <code>frame_array</code> is 8 bytes and <code>frame_array[0]</code> is 16 bytes then <code>num_frames</code> is going to be 0.</p>
  
  <p>But the question is how can size of an array be smaller than one of its elements? I heard about byte padding.</p>
  
  <p>I don't have much of an idea if it is causing the problem. Here is one of the links I referred to:
        <a href="https://stackoverflow.com/questions/1898657/sizeof-array-of-structs-in-c">Result of &#39;sizeof&#39; on array of structs in C?</a></p>
  
  <p>Although I have found a few workarounds to determine the size of an array of structs.</p>
  
  <ol>
  <li><p>Get the no. of elements from the caller and </p></li>
  <li><p>another is compulsorily get the last struct element as <code>{0,0,0,NULL}</code> and then
  in <code>write()</code> check for it's presence and stop scanning <code>frame_array</code> any further.</p></li>
  </ol>
  
  <p>But both depend on the caller, something which you can't trust.
  So where is the real problem. And how could I determine the value of <code>num_frames</code>?</p>