Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8632789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:23:59+00:00 2026-06-12T09:23:59+00:00

In an effort to learn pure C (coming from C++), I’ve decided to write

  • 0

In an effort to learn pure C (coming from C++), I’ve decided to write a simple math library using structs and macros.

So far, I have this as a test macro:

#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;

In my calling code, I have this:

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 => %f; y => %f \n", v.x, v.y );

    return 0;
}

My questions are as follows:

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 MulVec2(dest,src) macro assumes the objects passed in aren’t dynamically allocated, but I’d like support for that as well.

2) When I compile the code, I get this error:

../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'

What can I do to fix this?

Edit

I should have clarified that I don’t plan to use just 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 .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:24:00+00:00Added an answer on June 12, 2026 at 9:24 am

    As others pointed out, macros are not functions, and this would be a use case for a function. A macro simply textually expands Mul2Vec(…) to what you wrote in the macro, which fails to make any sense to the compiler, since one can’t return out of an expression.

    If you want to achieve the effect of C++ templates in C, reconsider your strategy, perhaps to simply choose one of float or double and stick to it. But if you absolutely must do generics without code duplication, you can define function-defining macros like this:

    /* vec-impl.h */
    
    #define PASTE(a, b) a ## b
    #define NAME(prefix, type) PASTE(prefix, type)
    
    #define VEC_T NAME(vec_, T)
    
    typedef struct {
      T x;
      T y;
    } VEC_T;
    
    void NAME(MulVec2_, VEC_T)(VEC_T *dest, VEC_T *src) {
     dest->x = dest->x * src->x;
     dest->y = dest->y * src->y; 
    }
    

    Compilation units that define actual functions would look like this:

    /* vec-double.c */
    
    #define T double
    #include "vec-impl.h"
    
    /* vec-float.c */
    
    #define T float    
    #include "vec-impl.h"
    

    This setup gives you a poor man’s approximation of C++ templates, without type inference and any of the fancy metaprogramming features. Again, don’t go that route unless you absolutely have to, and even then, minimize the code that does it. This is not idiomatic C code, and will not be well-received by competent C programmers—while C is more open to preprocessor hacking than C++, this is well over the line.

    Regarding the code you posted: to get a feeling of what macros are, get a good book on C that explains the topic, such as Kernighan and Ritchie’s “The C Programming language”. When exploring macros, please keep in mind the following:

    • Unless you really know what you are doing, never return out of a macro. If you do it anyway, make the word RETURN part of the macro name. The same goes for other flow-control statements, such as break, continue, and goto. People maintaining that code after you will appreciate it.

    • By convention, macros are always spelled with all-caps, so it’s MUL_VEC2, not MulVec2. This alerts the reader that he’s dealing with a macro, not a function.

    • The point of parentheses in macro definition is for the macro expansion to work even if passed a compound expression. Therefore it’s dest that must be parenthesized, not dest.x. I.e. instead of (dest.x), one would write (dest).x.

    • Use the -E switch to the compiler to show you the preprocessor output, so you can see what the compiler sees and figure out what’s going on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the effort to learn more about applets and Java, I am experimenting making
In an effort to learn about synchronization via Java, I'm just messing around with
I am making the effort to learn Vim. When I paste code into my
In an effort to lean Rx I'm writing a simple application which imitates reading
After going through the effort of finally purging distro ruby packages from my Ubuntu
I'm putting in some effort to learn Python, and I am paying close attention
In my effort to learn more about cross-platform developping in C# I tried checking
In an effort to learn more about networking I'd like to do an exercise:
In a seemingly never ending effort to learn more about iphone development, I have
I'm looking for a simple JavaScript library or framework to create interactive 2D animations

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.