Hi can someone explain to me why i get the results

1: 4 3
2: 3 4
3: 3 4
4: 3 4
5: 3 3
6: 3 4

for the following code?

 #include<stdio.h>



void switch2(int *a, int *b)
{
    int hold;
    hold = *a;
    *a = *b;
    *b = hold;
    return;
}

void switch3(int *a, int *b)
{
    int *hold;
    hold = a;
    a = b;
    b = hold;
    return;
}

int main()
{
    int x=3, y=4, *p=&x, *q=&y, *hold;
    switch2(&x, &y);
    printf("1: %d %d\n",x,y);
    switch2(p,q);
    printf("2: %d %d\n", *p, *q);
    switch3(&x,&y);
    printf("2: %d %d\n",x,y);
    switch3(p,q);
    printf("4: %d %d\n", *p, *q);

    hold = p;
    p = q;
    q = hold;
    printf("5: %d %d\n", *p, *q);
    printf("6: %d %d\n", x,y);
    return 0;
}

(Sorry im unable to code wrap.the buttons seem to be stuck when i clikc on them)

switch() is just swapping the variable addresses (which are local to switch funcction), instead of the values stored at those addresses

void switch(int* x, int* y)
{
   int hold = *x;
   *x = *y;
   *y = hold;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.