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

  • SEARCH
  • Home
  • 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 105235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:24:26+00:00 2026-05-11T01:24:26+00:00

Calling all C macro gurus… Is there any way to write a C macro

  • 0

Calling all C macro gurus…

Is there any way to write a C macro that will replace something like this:

my_var = 5; 

with this:

setVar(&my_var, 5); 

In other words, can I write a C macro that will override assignments for a specific variable (in the above example, my_var) and instead pass it to a function whose job it is to set that variable? If possible, I’d like to be able to hook into assignments of a specific variable.


EDIT: After thinking about this some more, I’m not sure it could be done. Even if you can come up with a macro to do it, setVar wouldn’t necessarily know the type of the variable its setting, so what would be the type of its second argument?


EDIT: The reason I’d like to hook assignments of specific variables is for use in a primitive debugger for some specialized embedded C code. It would be nice to be able to have a ‘watch list’, essentially like you have in an IDE. My first instinct was to try to hook variable assignments with a C macro so you could just drop the macro into your code and have that variable ‘watched’, but then again I’ve never really written a debugger before so maybe I’m going about that all wrong.

  • 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. 2026-05-11T01:24:26+00:00Added an answer on May 11, 2026 at 1:24 am

    Not with the standard preprocessor. It cannot change the parsing of the file, only replace proper names with a piece of code (and ‘=’ isn’t valid in a name).

    If you’re feeling adventurous, you can try to replace the executable ‘cpp’ with a small script which pre-processes the source code. But that might wreck havoc with the debugging information (file name and, if you’re replacing one line of code with several, with line number information, too). The script would call ‘sed’`:

    sed -e 's/my_var\s*=\s*([^;]+);/MY_VAR(my_var, $1);/' file.c > file_tmp.c 

    But your best bet is probably to put this into a script and simply run it on all your sources. This will change the code and you’ll see what is happening in your debugger.

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

Sidebar

Related Questions

Is there a way to do something like lexical closures using macrolet? What I
Will calling close on my WCF service kill all resources or set them up
I'm wondering if there's a simple way for a Word macro to determine which
Calling Validate() on an XmlDocument requires passing in a ValidationEventHandler delegate. That event function
I try to make a useful macro for logging. But I find that NSLog
I don't think this is technically a macro but I don't know what else
Lots of developers think that testing private methods is a bad idea. However, all
I have a class that I am using all over the place in my
Possible Duplicate: [F#] How to have two methods calling each other? Hello all, I
When calling CoInitializeEx , you can specify the following values for dwCoInit : typedef

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.