# pointers

This is a discussion on pointers within the C Programming forums, part of the General Programming Boards category; Hello, this is a problem from a written homework assignment: i have to draw out a stack model for this ...

1. ## pointers

Hello, this is a problem from a written homework assignment:
i have to draw out a stack model for this

Code:
```void foo(int **i, int **j){
int *temp;

if (**i < **j) {
temp = *i;
*i = *j;
*j = temp;
}
}

int main() {

int a = 3;
int b = 4;
int *x, *y;
x = &a;
y = &b;

foo(&x, &y);

printf("a = %d\n", a);
printf("b = %d\n", b);

printf("*x = %d\n", *x);
printf("*y = %d\n", *y);```
As a hint, he gave us the output:
--output--
a=3
b=4
*x = 4
*y = 3

I have a = 3, b = 4 and x points to a(3) and y points to b(4). however, when foo is called, i dont understand the double pointers. Somehow, judging by the output, the x and y pointers are switched using i and j. any help would be appreicated

2. Double pointer is a variable like any other. What is significant is that it's store an adress of pointer variable.
for example if you want to swap two integer variables with function you need to use two pointer to type int ( pass by reference). What if you want to swap two pointers? You just need to to use two double pointer to type int and so on.
Key is to think about pointer variable and also double pointer like like an ordinary variable.

3. hmm ok, but does i point to x or does i point to what x points to?

4. Ok this is basically your assigment chain
Code:
```a = 3;
x = &a;
i = &x;```
So we can see that a is equal to 3.
We can see that x points to a.
We can see that i points to x which points to a.

So if I had x and wanted the value stored by a, I would dereference it once, aka
Code:
`*x = 3;`
So if I had i and wanted to get what x was I would dereference it once, aka
Code:
`*i = x;`
However we know that x points to a, so to get to a from i we would need to dereference it twice, aka
Code:
`**i = a = 3;`
edit - You could look at it like this, we know:
Code:
```*x = a
*i = x```
So some simple substitution:
Code:
`*(*i) = a;`
Which is where we wound up above: **i = a;

5. ok thanks