# Thread: Pointers: & and * when scanning

1. ## Pointers: & and * when scanning

Hello, I have a quick question about using & and * when it comes to pointers and scanf.

If I have

Code:
```int *ptr;
int a = 3, b;```
What happens if I have

Code:
```ptr = &b;
scanf("%d", &ptr);```
where there's a & in front of the pointer

or

Code:
```ptr = &b;
scanf("%d", *ptr);```
where there's a * in front of the pointer?

I know that when you scan something into an array, you don't need the & because it's a pointer. So that means that when you scan something into a pointer, you don't need it. But what happens if you have a & or a * in front of ptr? Does * mean that the value that is scanned in s put into b?

Pointers are variables too, with their own address in memory. In this case, you print the address of "ptr".

"ptr" is... a pointer, so you can dereference it to get they value it's pointing at. In this case garbage, because "b" was left undefined.

You can think of all pointers as arrays and vice-versa. Whether that array has size 0(NULL), 1, 10 or 42 is up to the programmer. "*ptr" is equivalent to "ptr[0]", "*(ptr+1)" to "ptr[1]" etc. When you do "*ptr = 53;" you're changing a value inside that array. When you do "ptr = &b" you are actually changing the array itself.

So in the case of scanf, which is what I have up there, where am I scanning in values when I use &ptr or *ptr.
I'm not printing in this case.

4. Originally Posted by blh
So in the case of scanf, which is what I have up there, where am I scanning in values when I use &ptr or *ptr.
Let's assume that "ptr" exists at address 0x100000, "a" at address 0x100008 and "b" at address 0x10000C. I will denote what exists at each address using an arrow.

Before assignment:
0x100000 -> garbage
0x100008 -> 3
0x10000C -> garbage

After assignment:
0x100000 -> 0x100008
0x100008 -> 3
0x10000C -> garbage

"&ptr" gives the leftmost value
"ptr" gives the rightmost value
"*ptr" gives the rightmost value of the address that exists at its own rightmost value( it points to the value ). You can visualize it as:
0x100000 -> 0x100008 -> 3

5. It's actually undefined what will happen, because your object types, *ptr and &ptr, do not match your conversion specifier, %d.

But to answer your question, suppose scanf() does actually attempt to store the converted input, and you entered 55:

With the correct statement,
Code:
`scanf("%d", ptr);`
the value 55 would be stored in b.

With &ptr as an argument, it will try to store the value 55 in ptr.
Now ptr, in all likelyhood, no longer points to b.

With *ptr as an argument, it will try to store the value 55 in whatever b is pointing to.
This is where the real havoc can be wrought.

-

6. Originally Posted by GReaper
"&ptr" gives the leftmost value
"ptr" gives the rightmost value
"*ptr" gives the rightmost value of the address that exists at its own rightmost value( it points to the value ). You can visualize it as:
0x100000 -> 0x100008 -> 3
Okay I see. That helps, thank you!

7. Originally Posted by megafiddle
With &ptr as an argument, it will try to store the value 55 in ptr.
Now ptr, in all likelyhood, no longer points to b.

With *ptr as an argument, it will try to store the value 55 in whatever b is pointing to.
This is where the real havoc can be wrought.

-
Yeah I didn't expect for these statements to do anything specific, it was just a to get me thinking, and I have little experience with pointers. Thank you!