macros

This is a discussion on macros within the C Programming forums, part of the General Programming Boards category; Hi, could anyone help me please? i'm not sure how to write a swap(t, x, y) macro that interchange the ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    12

    macros

    Hi, could anyone help me please? i'm not sure how to write a swap(t, x, y) macro that interchange the x and y arguments of type t...

    Thanks;

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Sounds like a homework assignment. Well, why don't you show us what you have so far?
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    12
    It's not a homework assignment. I just started C programming and i wanted to know how to write functions in macro that can allow any data type that's all.

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Well, what do you have? Do you know how to make a macro? If so try something and post and I am sure more people will help you from there.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    12
    Well, i just know to write a function like this...
    Code:
     
     #define isBigger(a,b) (((a) > (b))? (a):(b))
    But i'm not sure how to do a swap function...

  6. #6
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Well, a swap function typically involves 3 variables; the two you are swapping and one used as a temp store. To perform a swap in code you would(assuming you had three variables named x, y, z and you wanted to swap x and y):
    Code:
         z = x
         x = y
         y = z
    And that would swap the values of variables x and y. Now just attempt to apply that to a macro and you'll be there.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    12
    i see, so is this correct?
    Code:
      #define swap(t, a, b) (t temp=a; a=b; b=temp;)

  8. #8
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    yep, just what exactly are you hoping to use, either t or temp. It has to be one or the other. According to you macro declaration it should be t so:
    Code:
    #define swap(t, a, b) (t =a; a=b; b=t;)
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    12
    sorry, i confussed you. i wanted the t enter in the swap() to be the type t of the a and b arguments. Is it possible to do something like that?

  10. #10
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    I believe you can only create generic types like that in C++ using templates. For you problem I would just have the programmer pass the macro three variables like so:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define swap(x, y, z) (z) = (x); (x) = (y); (y) = (z);
    
    int main()
    {
    	int x = 0, y = 1, z;
    	float a = 2.0, b = 3.5, c;
    
    	printf("The value before swap of x= %i, y= %i\n", x, y);
    	swap(x, y, z);
    	printf("The value after swap of x= %i, y= %i\n", x, y);
    	
    	printf("The value before swap of a= %f, b= %f\n", a, b);
    	swap(a, b, c);
    	printf("The value after swap of a= %f, b= %f\n", a, b);
    	
    	getchar();
    	return 0;
    }
    I will look around some more and see if I can find a reference to templating in C but I do not believe it can be done like how you want it to.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    12
    Thanks for your help

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    http://www.eskimo.com/~scs/C-faq/q10.3.html
    http://www.eskimo.com/~scs/C-faq/q10.4.html
    Code:
    #include <stdio.h>
    
    #define swap(t,x,y)  \
    do {                 \
       t a = (x);        \
       (x) = (y);        \
       (y) = a;          \
    } while(0)
    
    int main(void)
    {
       int i = 42, j = -1, k = 9, *p = &i, *q = &j;
       float f = 1.23, g = 4.56;
       printf("i = %d, j = %d | k = %d | f = %g, g = %g | p = %p, q = %p\n",
              i, j, k, f, g, (void*)p, (void*)q);
       swap(int, i, j);
       swap(float, f, g); 
       swap(int, k, k);
       if ( k )
          swap(int*, p, q);
       else
          puts("message");
       printf("i = %d, j = %d | k = %d | f = %g, g = %g | p = %p, q = %p\n",
              i, j, k, f, g, (void*)p, (void*)q);
       return 0;
    }
    
    /* my output
    i = 42, j = -1 | k = 9 | f = 1.23, g = 4.56 | p = 0012FF88, q = 0012FF84
    i = -1, j = 42 | k = 9 | f = 4.56, g = 1.23 | p = 0012FF84, q = 0012FF88
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Apr 2005
    Posts
    12
    Thanks for ur solution!

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Nice, but can you spot the bug in Dave's solution?
    There's a hint in the clc FAQ
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Apr 2005
    Posts
    12
    Thanks for the tip

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 06:52 PM
  2. Pre-processor macros causing segfaults
    By nempo in forum C++ Programming
    Replies: 6
    Last Post: 02-10-2009, 01:35 AM
  3. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 02:51 AM
  4. Macros vs Inline Functions
    By vb.bajpai in forum C Programming
    Replies: 4
    Last Post: 08-02-2007, 11:51 AM
  5. template fn replacements for msg macros
    By Ken Fitlike in forum Windows Programming
    Replies: 17
    Last Post: 10-30-2002, 06:55 AM

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