title: C Macros - Pass by Pointer vs Copy/Errors in passing to macro
body: <p>In an effort to learn pure C (coming from C++), I've decided to write a simple math library using structs and macros.</p>
  
  <p>So far, I have this as a test macro:</p>
  
  <pre><code>#define MulVec2(dest,src) ((dest.x) = (dest.x) * (src.x); (dest.y) = (dest.y) * (src.y); return dest;)
  
  typedef struct vec2f_s
  {
      float x, y;
  }
  vec2f_t;
  </code></pre>
  
  <p>In my calling code, I have this:</p>
  
  <pre><code>int main(void)
  {
  
      vec2f_t v, w;
  
      v.x = 5.0f;
      v.y = 2.0f;
  
      w.x = 3.0f;
      w.y = 3.0f;
  
      v = MulVec2(v, w);
  
      printf( "x =&gt; %f; y =&gt; %f \n", v.x, v.y );
  
      return 0;
  }
  </code></pre>
  
  <p>My questions are as follows:</p>
  
  <p>1) Do I need to write a separate macro for passing pointers/addresses of objects to a macro? If so, how? For instance, note that the <code>MulVec2(dest,src)</code> macro assumes the objects passed in aren't dynamically allocated, but I'd like support for that as well.</p>
  
  <p>2) When I compile the code, I get this error:</p>
  
  <pre><code>../main.c: In function 'main':
  ../main.c:15:9: error: expected ')' before ';' token
  ../main.c:15:7: error: incompatible types when assigning to type 'vec2f_t' from type 'float'
  </code></pre>
  
  <p>What can I do to fix this?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I should have clarified that I don't plan to use <em>just</em> macros for this, but the reason for writing a macro is so I don't have to write a separate function for double and float variations of the vectors. I'd like it to be as reusable as possible by following the DRY (don't repeat yourself) principle .</p>