title: Storing void pointers in a struct
body: <p>I'm getting segmentation fault on code that is trying to initialize a struct of pointers to 0mq context and socket. The commented out code in the main method works, but it's only using local variables. I would like to initialize them and pass them around in a struct, but my google foo is failing me on how to do this properly.</p>
  
  <pre><code>#include "zhelpers.h"
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;zmq.h&gt;
  
  struct publisher{
      void *handle;
      void *context;
  };
  
  void init_publisher(struct publisher *p);
  void destroy_publisher(struct publisher *p);
  void publish(struct publisher *p,char *msg);
  
  void init_publisher(struct publisher *p)
  {
      p = (struct publisher *)malloc(sizeof(struct publisher));
      p-&gt;context = malloc(sizeof(void *));
      p-&gt;handle = malloc(sizeof(void *));
      void *context = zmq_ctx_new();
      void *handle = zmq_socket(context,ZMQ_PUB);
      zmq_bind(handle, "tcp://*:5556");
      zmq_bind(handle, "ipc://feed.ipc");
      p-&gt;context = context;
      p-&gt;handle = handle;
  }
  
  void destroy_publisher(struct publisher *p)
  {
      zmq_close(p-&gt;handle);
      zmq_ctx_destroy(p-&gt;context);
      free(p-&gt;handle);
      free(p-&gt;context);
      free(p);
  }
  
  void publish(struct publisher *p,char *msg)
  {
      s_send(p-&gt;handle, msg);
  }
  
  int main(void)
  {
  /**
      void *context = zmq_ctx_new();
      void *publisher = zmq_socket(context, ZMQ_PUB);
      int rc = zmq_bind(publisher, "tcp://*:5556");
      assert(rc == 0);
      rc = zmq_bind(publisher, "ipc://weather.ipc");
      assert(rc == 0);
      printf("Started Weather Server...\n");
  
      srandom((unsigned) time (NULL));
      int zipcode, temperature, relhumidity;
      zipcode = randof(100000);
      temperature = randof (215) - 80;
      relhumidity = randof (50) + 10;
  
      char update[20];
      sprintf(update, "%05d %d %d", zipcode, temperature, relhumidity);
      s_send(publisher, update);
      zmq_close(publisher);
      zmq_ctx_destroy(context);
  */
  
      struct publisher *p;
      init_publisher(p);
      printf("Setup pub\n");
  
      srandom((unsigned) time (NULL));
      int zipcode, temperature, relhumidity;
      zipcode = randof(100000);
      temperature = randof (215) - 80;
      relhumidity = randof (50) + 10;
      char update[20];
      sprintf(update, "%05d %d %d", zipcode, temperature, relhumidity);
      publish(p,update);
      printf("Published Message\n");
  
      destroy_publisher(p);
      printf("Destroyed publisher\n");
      return 0;
  }
  </code></pre>