Thread: macro problem

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    30

    macro problem

    if I have a macro defined as

    Code:
    #define swap(a,b,c) c t; t=a,a=b,b=t;
    The above would work if a,b were numbers and 'c' was a data type like int.but it wont work for float * or any other pointer type.it will give a compile error ,why?


    cheers!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You would need to show an example use of "it doesn't work".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Salem View Post
    You would need to show an example use of "it doesn't work".
    I'd be more interested in where it does work...

    Have you seen this one:
    Code:
    #define swap(a,b) {a ^= b; b ^= a; a ^= b;}
    That only works with ints, tho.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    I'd be more interested in where it does work...

    Have you seen this one:
    Code:
    #define swap(a,b) {a ^= b; b ^= a; a ^= b;}
    That only works with ints, tho.
    So far as I can tell it works everywhere for anything, provided you are in a context where swap can safely expand to two statements (so if it's inside an if, you're going to need braces), at least assuming C99 so we can have variables declared anywhere.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    I'd be more interested in where it does work...
    I think that it should work in quite a few situations, but to enable it to work in more situations:
    Code:
    #define swap(a,b,c) do { c t; t=a,a=b,b=t; } while (0)
    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

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    I think that it should work in quite a few situations, but
    How would I call that then? I almost never use macros, so I am interested in the syntax. But if I try:
    Code:
    swap(x,y,z);
    Where x y z are ints, I get:

    error: ‘t’ undeclared (first use in this function)

    for both versions of this. Of course, this will work:
    Code:
    #define swap2(a,b,t) {t=a,a=b,b=t;}
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    swap generally involves 2 variables. In the macro, the third parameter specifies the type, so if x and y are ints, you would call it swap(x, y, int)

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    How would I call that then?
    Eh, eklavya8 described an example: "The above would work if a,b were numbers and 'c' was a data type like int."

    Thus the example would be:
    Code:
    swap(x, y, int);
    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

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Got it, thanks (I was thinking, "t" for temp, and that the OP meant "submit a tmp variable of the correct type")!

    So today is my day to learn about writing macros...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    So far as I can tell it works everywhere for anything, provided you are in a context where swap can safely expand to two statements (so if it's inside an if, you're going to need braces), at least assuming C99 so we can have variables declared anywhere.
    error: invalid operands to binary ^ (have ‘float’ and ‘float’)

    Code:
    #include <stdio.h>
    
    #define swap(a,b) {a ^= b; b ^= a; a ^= b;}
    
    int main() {
    	float i=0.7f, j=22.11f;
    	swap(i,j);
    	printf("swap: %f %f\n",i,j);
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    MK27, tabstop's statement was in response to your comment about wanting to know where eklavya8's swap macro will work. It was not in response to your comment about XOR swap.
    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

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by MK27 View Post
    error: invalid operands to binary ^ (have ‘float’ and ‘float’)

    Code:
    #include <stdio.h>
    
    #define swap(a,b) {a ^= b; b ^= a; a ^= b;}
    
    int main() {
    	float i=0.7f, j=22.11f;
    	swap(i,j);
    	printf("swap: %f %f\n",i,j);
    	return 0;
    }
    Because the bitwise operators work only on integral operands (singed or unsigned).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in macro
    By Bargi in forum C Programming
    Replies: 17
    Last Post: 02-04-2009, 10:17 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM