how the macro works

This is a discussion on how the macro works within the C Programming forums, part of the General Programming Boards category; Hello everyone, I can not understand how the following code works and assign 100 to counter variable? Code: void Foo ...

  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
    21,659
    What's with the , [comma ]operator here?
    Rather superfluous in this context, methinks.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,659
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,659
    rdrast, George2 does that all the time, I'm getting used to it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    99
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21