title: Distribute a structure using MPI_Allgather
body: <p>I  have to send a structure to all processes using <code>MPI_Allgather()</code>. I don't seem to get any apparent error, however the code doesn't work. When I check if I have received any values in <code>recv[]</code>, it shows none. If I just send a single variable instead of the structure using similar code, that works, so I am not sure what is going on. The structure has static arrays, so the memory should be contiguous, or should I be using <code>MPI_Pack</code> or something?  Here is the code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include "mpi.h"
  
  #define NUMEL 21
  struct mystruct{
   int sendarray[10];
   int a;
   char array2[10];
  };
  
  typedef struct mystruct struct_t;
  
  int main (int argc, char ** argv)
  {
   MPI_Status status;
   int rank, size;
   char *recv;
   int i, j;
   MPI_Init(&amp;argc, &amp;argv);
   MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank);
   MPI_Comm_size(MPI_COMM_WORLD, &amp;size);
   // init
   struct_t * fd = (struct_t*)malloc(sizeof(*fd));;
   for (i=0;i&lt;10;i++){
      fd-&gt;sendarray[i] = 0;
      fd-&gt;array2[i] = 0;
   }
   recv = (char *) malloc ( size*NUMEL);
  
   // put some stuff in your array
   for (i=0;i&lt;size;i++){
      if(rank == i){
        fd-&gt;sendarray[i] = i *10;
        fd-&gt;array2[i] = i *20;
        fd-&gt;a = rank;
     }
    if(fd-&gt;sendarray[i] != 0)
       printf("My rank is %d, fd-&gt;sendarray[%d]  is %d\n", rank, i, fd-&gt;sendarray[i]);
       }
  
     // gather data from all now..
     MPI_Allgather (fd, NUMEL, MPI_BYTE, recv, NUMEL * size, MPI_INT, MPI_COMM_WORLD);
  
    // check if correct data has been received
    for (i=0;i&lt;size*NUMEL;i++){
     if(recv[i] != 0)
       printf("My rank is %d and recv[i]=%d and i is %d\n", rank, recv[i],i);
    }
  MPI_Finalize();
  }
  </code></pre>