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 8074137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:33:22+00:00 2026-06-05T14:33:22+00:00

I have a function-like macro that takes in an enum return code and a

  • 0

I have a function-like macro that takes in an enum return code and a function call.

#define HANDLE_CALL(result,call) \
    do { \
        Result callResult = call; \
        Result* resultVar = (Result*)result; \
        // some additional processing
        (*resultVar) = callResult; \
    } while(0)

Does fancy pointer casting to Result* and subsequent de-referencing gain you anything? That is, is there any advantage to doing this over just:

callResult = call;
// additional processing
*result = callResult;

The macro is used like this:

Result result;
HANDLE_CALL(&result,some_function());

By the way, this isn’t my code and I’m not a power C user so I’m just trying to understand if there is any logic behind doing this.

  • 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-05T14:33:24+00:00Added an answer on June 5, 2026 at 2:33 pm

    I think what it gives you, is that the user of the macro can pass in a pointer of any old type, not just Result*. Personally I’d either do it your way, or if I really wanted to allow (for example) a void* macro argument I’d write *(Result*)(result) = callResult;.

    There’s another thing it might be, depending what the rest of the macro looks like. Does “some additional processing” mention resultVar at all, or is the line (*resultVar) = callResult; conditional? If so, then resultVar exists in order to ensure that the macro evaluates each of its arguments exactly once, and therefore behaves more like a function call than it would if it evaluated them any other number of times (including zero).

    So, if you call it in a loop like HANDLE_CALL(output++, *input++) then it does something vaguely predictable. At least, it does provided that output is a Result* as the macro author intended. I’m still not sure what the cast gives you, other than allowing different argument types like void* or char*.

    There are some situations where it could make another difference whether you have that extra cast or not. For example, consider:

    typedef double Result;
    
    int foo() { return 1; }
    int i;
    HANDLE_CALL(&i, foo());
    

    if typedef double Result; isn’t visible on screen, the other three lines appear pretty innocuous. What’s wrong with assigning an int to an int, right? But once the macro is expanded, bad stuff happens when you cast an int* to double*. With the cast, that bad stuff is undefined behavior, most likely double is bigger than int and it overruns the memory for i. If you’re lucky, you’ll get a compiler warning about “strict aliasing”.

    Without the cast you can’t do double* resultVar = &i;, so the original macro with that change catches the error and rejects the code, instead of doing something nasty. Your version with *result = callResult; actually works, provided that double can accurately represent every value of int. Which with an IEEE double and an int smaller than 53 bits, it can.

    Presumably Result is really a struct, and nobody would really write the code I give above. But I think it serves as an example why macros always end up being more fiddly than you think.

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

Sidebar

Related Questions

I have some existing code that uses an enum instead of a function pointer
I have some debugging code that looks like the following: #define STRINGIFY(x) #x #define
In an Excel macro, I have a function defined like to return a Recordset
I have a function like this function login_redirect($redirect, $vars = array()) { return $redirect;
I want to have a macro dbtest that can be used like this: (dbtest
I have some 3d interpolation code that takes up 90% of my projects runtime
I have function like this: function gi_insert() { if(!IS_AJAX){ $this->load->library('form_validation'); $this->form_validation->set_rules('name', 'Naslov', 'trim|required|strip_tags'); $this->form_validation->set_rules('body',
let say i have function like below function doSomethingNow(){ callSomethingInFutureNotExistNow(); } at the moment
I am trying to learn JavaScript... I have function like, I take the format
I have a function like the following: // Convert To JPG // public string

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.