title: Which way is better for creating type-agnostic structures in C?
body: <p>I'm trying to write some generic structures. Essentially, what I need for my purpose is C++ templates, but since I'm writing in C, templates are out of consideration. Currently I'm considering 2 ways of achieving what I want.</p>
  
  <p>Method 1: use the preprocessor. Like so:</p>
  
  <pre><code>#define DEFINE_PAIR(T) typedef struct Pair_##T{ \
                             T x; \
                             T y; \
                          } Pair_##T
  
  DEFINE_PAIR(int);
  
  int main(){
     Pair_int p;
     return 0;
  }
  </code></pre>
  
  <p>An obvious downside to it is that you have to invoke the macro before using the type. Probably there are more disadvantages, which I hope you will point out.</p>
  
  <p>Method 2: just use void-pointers, like so:</p>
  
  <pre><code>typedef struct Pair{
     void* x;
     void* y;
  } Pair;
  </code></pre>
  
  <p>Obviously, this approach is not type safe (I could easily pass a pair of strings to a function expecting a pair of doubles), plus the code doing deallocation gets a lot messier with this approach.</p>
  
  <p>I would like to hear your thoughts on this. Which of the two methods is better/worse and why? Is there any other method I could use to write generic structures in C?</p>
  
  <p>Thanks.</p>