Thread: Difference between macro and pass by reference?

  1. #1
    Registered User
    Join Date
    Nov 2001

    Difference between macro and pass by reference?

    Like the title says, what is the difference between using a macro, which just textually substitutes whatever, as opposed to just writing a function with reference parameters? I know there must be a difference, or else there would be no need for macros. Can someone please help me to understand the difference, possibly with an example? TIA.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Macros shouldn't be used as functions - it's a C-style to make something inline. It's a bit harder to debug and off use due the inlines in C++.

    #define foo(x) x+  // see the error
    int main()
       int y;
       foo(y);  // This is the line you get the error
       char s[] = "Hello world";
       foo(s);  // Want to know what happens?
    inline void foo(const int &x)
      x+;  //  Now you'll get the error here
    int main()
       int x;
       char s[] ="dlrow olleH";
       foo(s);  //  And another one here
    kooma -

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Inline functions also enforce type safety.........

    Also #defines may never actually reach the compiler as they are converted by the preprocessor beofre compilation.....I have read of occasions where this can give problems debugging....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculation of sizof through macro. Possible?
    By freeindy in forum C Programming
    Replies: 4
    Last Post: 09-24-2007, 03:05 PM
  2. macro function
    By bradleyd in forum C Programming
    Replies: 8
    Last Post: 05-21-2007, 05:18 PM
  3. Passing Keys (macro program)
    By Coder87C in forum C Programming
    Replies: 3
    Last Post: 07-08-2003, 06:54 PM