Thread: how the macro works

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    how the macro works

    Hello everyone,


    I can not understand how the following code works and assign 100 to counter variable?

    Code:
    void Foo (int* input)
    {
    	*input = 100;
    
    	return;
    }
    
    #define GETFOO  (Foo( &counter ), counter)
    
    int counter;
    
    int main()
    {
    	GETFOO;
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you clarify what you don't understand?

    The assignment, to me, is quite straightforward: It passes a pointer to counter, which is then assigned 100 in the Foo function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Location
    Bangalore, India
    Posts
    24
    Code:
    #define GETFOO  (Foo( &counter ), counter)
    What's with the , [comma ]operator here?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What's with the , [comma ]operator here?
    Rather superfluous in this context, methinks.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Rather superfluous in this context, methinks.
    Yes, but if you where to do something like:
    Code:
    int x = GETFOO;
    it would lead to x being assigned the value of counter.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matsp View Post
    Yes, but if you where to do something like:
    Code:
    int x = GETFOO;
    it would lead to x being assigned the value of counter.

    --
    Mats
    Yes, it would not be superfluous in such a context, but I think this whole thing is rather suspect... a code obfuscation contest?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Yes, it would not be superfluous in such a context, but I think this whole thing is rather suspect... a code obfuscation contest?
    I _HAVE_ seen code that actually does this type of thing - slightly more complex in fact - but I'd rather see it implemented as an inline function if at all possible. The code where I saw it actually used some textual replacement in the macro, so a function wouldn't quite work in that case. It was kernel code, so probably not really intended for "easy readability" tho'.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    rdrast, George2 does that all the time, I'm getting used to it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    100
    LaserLight, I suppose I should, but honestly... That kind of spamming inclines me to simply ignore anything I see by him, and others that do the same.

    Guess I'm just in a general bad mood this day, and that set me off

    Be well.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    rdrast, George2 does that all the time, I'm getting used to it.
    Ok, so perhaps we should stop answering such questions - as there's probably an equally good answer floating about somewhere else. It is really annoying when you find that someone else posted the same answer 5 hours ago, when you have spent some considerable effort to explain something [not necessarily the case in this particular thread, but...]


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Can anybody show me why this works??
    By tzuch in forum C Programming
    Replies: 2
    Last Post: 03-29-2008, 09:03 AM
  3. Macro Program
    By daspope in forum Windows Programming
    Replies: 5
    Last Post: 04-25-2004, 04:02 AM
  4. about Makefile and Macro
    By tom_mk in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2003, 01:07 PM
  5. Minimum Macro
    By COBOL2C++ in forum C++ Programming
    Replies: 6
    Last Post: 09-10-2003, 10:16 AM