# Thread: Ramoth's bumped pointer question (was How did you...)

1. I (newbie) have a question on pointers. I'll paste the code. I know how the main sends to the call, but after that I'm lost. Can anyone explain what happens? The program runs and the execution is: "In hi and d = 16"; "In hi again and d = 23"; "In Main and a=8, b=15, c=5". So I know the answers but no clue how it works

Code:
```#include <stdio.h>
void ho(int *k, int l, int *m);
void hi(int *x, int *y, int d);

int main(void)
{   int a,b,c;
c=5;
hi(&a, &b, c);
printf("In main and a=%d, b=%d, c=%d",a,b,c);
return (0);
}

void ho(int *k, int l, int *m)
{    *k = 5 * l;
*m = l + 20;
}

void hi(int *x, int *y, int d)
{    int a;
a=3;
*x = 8;
d = *x + d + a;
printf("In hi and d = %d\n", d);
ho(y, a, &d);
printf("In hi again and d = %d\n", d);
}```

2. hi(&a, &b, c);
This means that the address of a and b are sent, while the value of c is sent.
The former is called "passing by reference."

*x = 8;
Because you sent the address of x, you cannot reference it directly. You want the value of x to be 8, not the address, so you call * x.

Need more help?

Also, you should have started a new thread rather then bumped this one

3. Thanks MadCow. I think if this ever gets cleared up for me, I'll have a much better understanding of pointers.

I get the first send, and why the answer is 16.

But to clarify how it works, I'm trying to trace the program ...
The main passes address of a, b and value 5 to hi.

The "hi" function sends y, a, &d; or 8, 3, address of d, which now is 16, to "ho"

In ho, *k is 8, int l is 3, *m is 16
Since the address of d was sent to pointer to m, ho returns (*m=3+20 =23) to d in the 2nd printf in hi .

But back to Main - no lo comprende. Been trying to trace it in words but I'm confusing myself.

4. What values don't you understand in main? a, b, c or all of them?

c is easy. It's set to 5, then it's passed by value, so it never changes. a we can see is set to 8 in hi()
Code:
`*x = 8; // Remember x points to a`
then it never changes again. B, gets passed through to the function ho().

Remember, in main it's b, which is then pointed to by y in hi() which is then assigned to the pointer k in ho().

k = y ---> &b

But, the only time it's assigned anything is in ho().
Code:
`*k = 5 * l;`
At the time, you know l points to the a in hi, which equals 3, so dereferencing that will give you 5 * 3. Which equals 15.

5. Thanks a lot - that helps a great deal! Where I especially was confused is:
Code:
```void hi(int *x, int *y, int d)

{ ho(y, a, &d);```
Using y without the * passes value back to the *y pointer. I didn't get that.

Is this correct? Since the address of d was sent to pointer to m, ho returns (*m=3+20 =23) to d in the 2nd printf in hi .

Thanks Again!

6. Originally Posted by Ramoth
Is this correct? Since the address of d was sent to pointer to m, ho returns (*m=3+20 =23) to d in the 2nd printf in hi .
Yes, and no. Yes, because I know in your mind, you have the right idea. No, because you're use of certain words kind of fudges up that statement.

For future reference, ho() doesn't return anything. It's a void function. Arguments, whether passed by value or reference are not returned. A return is when the function resolves. Arguments passed to a pointer are resolved at assignment.

7. Thanks SlyMaelstrom - you're a big help

8. Arguments passed to a pointer are resolved at assignment.
Presumably you meant

"Arguments passed to a function are resolved at assignment."

I imagine this site's tutorials have something on pointers.