d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C Macro Swap Function > Can't Figure It Out
Add Reply New Topic New Poll
Member
Posts: 2,392
Joined: Oct 14 2007
Gold: 573.00
Mar 23 2014 01:49pm
I've been stuck with this problem forever, its a fill in the blank:

#define Swap____ {\
int ______;\
int ______;\
\
_______;\
_______;\
}

It's supposed to be kind of call by reference swap function..
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 23 2014 02:13pm
Code
#define Swap(x,y){ int t;t=x;x=y;y=t; }


This post was edited by Minkomonster on Mar 23 2014 02:16pm
Member
Posts: 2,392
Joined: Oct 14 2007
Gold: 573.00
Mar 23 2014 02:45pm
Quote (Minkomonster @ Mar 23 2014 01:13pm)
Code
#define Swap(x,y){ int t;t=x;x=y;y=t; }


That would be a pass value, so it doesn't work
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 23 2014 03:01pm
Quote (killedbyz @ Mar 23 2014 03:45pm)
That would be a pass value, so it doesn't work


Its a pre processor directive. It isn't going to pass by reference regardless. But if you really want it pass by reference then you can do

Code
#define Swap(x,y){ int t;t=*x;*x=*y;*y=t; }






This post was edited by Minkomonster on Mar 23 2014 03:02pm
Member
Posts: 2,392
Joined: Oct 14 2007
Gold: 573.00
Mar 25 2014 07:23pm
I think I know what it means. Here's the problem with that macro..

Say you're swapping index with array[index]
then the swap substitution would be:
int temp = (index); (index) = (array[index]); (a[index]) = __swapTemp

On the first operation index is changed, so when used on the second one it produces a bug
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Mar 25 2014 09:05pm
Quote (killedbyz @ 23 Mar 2014 14:49)
I've been stuck with this problem forever, its a fill in the blank:

#define Swap____ {\
  int ______;\
  int ______;\
\
  _______;\
  _______;\
}

It's supposed to be kind of call by reference swap function..


Trick question!

#define Swap(a, b, c)
Member
Posts: 71,481
Joined: Jun 15 2007
Gold: 11.11
Mar 25 2014 09:13pm


Code

#include <stdio.h>
#include <stdlib.h>

#define SwapPointer(x, y){ int t; t=*x; *x=*y; *y=t; }
#define SwapXOR(x, y) { x^=y; y^=x; x^=y; }
#define SwapNormal(x, y) { int t; t=x; x=y; y=t; }


int main()
{
int array[2] = {1, 2};

printf("Original:\t%d\t%d\n", array[0], array[1]);

SwapPointer(&array[0], &array[1]);

printf("Pointer:\t%d\t%d\n", array[0], array[1]);

SwapXOR(array[0], array[1]);

printf("XOR:\t\t%d\t%d\n", array[0], array[1]);

SwapNormal(array[0], array[1]);

printf("Normal:\t\t%d\t%d\n", array[0], array[1]);

return 0;
}



----------
Output
----------

Original: 1 2
Pointer: 2 1
XOR: 1 2
Normal: 2 1

Process returned 0 (0x0) execution time : 0.033 s
Press any key to continue.


What is the problem you hare having? I don't see the a bug in his code. How are you attempting to use this? Show us your code...

Member
Posts: 2,392
Joined: Oct 14 2007
Gold: 573.00
Mar 25 2014 11:38pm
Quote (Princess @ Mar 25 2014 08:13pm)
Code
#include <stdio.h>
    #include <stdlib.h>
   
    #define SwapPointer(x, y){ int t; t=*x; *x=*y; *y=t; }
    #define SwapXOR(x, y) { x^=y; y^=x; x^=y; }
    #define SwapNormal(x, y) { int t; t=x; x=y; y=t; }
   
   
    int main()
    {
        int array[2] = {1, 2};
   
        printf("Original:\t%d\t%d\n", array[0], array[1]);
   
        SwapPointer(&array[0], &array[1]);
   
        printf("Pointer:\t%d\t%d\n", array[0], array[1]);
   
        SwapXOR(array[0], array[1]);
   
        printf("XOR:\t\t%d\t%d\n", array[0], array[1]);
   
        SwapNormal(array[0], array[1]);
   
        printf("Normal:\t\t%d\t%d\n", array[0], array[1]);
   
        return 0;
    }
   
   
   
    ----------
    Output
    ----------
   
    Original:      1      2
    Pointer:        2      1
    XOR:            1      2
    Normal:        2      1
   
    Process returned 0 (0x0)  execution time : 0.033 s
    Press any key to continue.

   
    What is the problem you hare having? I don't see the a bug in his code. How are you attempting to use this? Show us your code...


The problem would be when doing something like

int main() {
int index = 1;
int array[2] = {55, 43};

swap(index, array[index]);
}

This post was edited by killedbyz on Mar 25 2014 11:39pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 26 2014 02:33am
Quote (killedbyz @ Mar 26 2014 12:38am)
The problem would be when doing something like

int main() {
    int index = 1;
    int array[2] = {55, 43};

    swap(index, array[index]);
}


Think about what you are trying to do...

This post was edited by Minkomonster on Mar 26 2014 02:57am
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Mar 29 2014 11:36am
nvm

This post was edited by SelfTaught on Mar 29 2014 11:37am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll