# Thread: Newbie needs help with pointers

1. ## Newbie needs help with pointers

Bah, pointers. I have come to the chapter in my book were I learn about pointers. I don't understand a single bit about em. Can someone help, and explain them? Thanks. Could you explain why this code works?

Code:
```int main(int argc, char *argv[])
{
int intVar;
int* pintVar;

pintVar = &intVar;
*pintVar = 10;

cout << pintVar <<  " is pIntVar\n";
cout << intVar <<  "is intVar\n";

return 0;
}```
All i get is pintVar as a hex and intVar as 10. I don't understand whats going on, how, and why. If anyone could help, that would be great. BTW, I"m using dev-c++

2. Instead of storing a value, a pointer stores the address to another variable (or some junk data if you're unlucky).
pintVar = &intVar;
Here you tell the pointer to point at the variable intVar. & returns the address of a variable.
*pintVar = 10;
Here you set what the pointer points at to 10. It points to intVar, so you set intVar to 10. The * tells that you set the value to what it points at. If you just wrote pintVar = 10; you would change the actual address the pointer points at to some probably invalid value.
cout << pintVar
Here you print the value of the pointer, which is the address it's pointing at. They are often expressed as hex numbers, as you've experienced.

To print the variable it points at, type:
cout << *pintVar

3. so is it the same thing as saying pintvar = intVar; ?

4. pintVar is equal to the address of intVar, or:
pintVar equals &intVar

whereas *pintVar equals intVar, because when you use *pintVar, it is just like you had intVar in there.

So *pintVar==10 (or *pintVar==intVar), where pintVar==&intVar

meh, i hope that didn't just confuse you more

5. so is it the same thing as saying pintvar = intVar; ?
No. pintvar holds the address of a variable that contains and int.

Think of it like this. Say you have a cat, I want you to show me where the cat is. So you point to the cat. All you are doing is telling me the location of the cat. You are the pointer. Through you I can get to the cat and make changes (aka a haircut).

so:
Code:
` int *pintvar;`
Is you
Code:
` int intVar;`
is the cat
Code:
` &intVar;`
is the location of the cat
Code:
` pintvar = &intVar;`
is having you point to the cat's location
Code:
` *pintvar = ANNOY_KITTY;`
is me going through you to get to the cat and make a change.

6. ok, get it!

7. lol @ Thantos, that's the best explanation I've heard

8. Originally Posted by Thantos
Code:
` *pintvar = ANNOY_KITTY;`
is me going through you to get to the cat and make a change.
hahaha... best code evar... great explanation too...

9. St0rmTroop3er,

Just in-case you are wondering why anyone would want to use a pointer, it's usually to get-around the fact the a function can only return one value. So, if you want your function to affect more than one variable, you can use pointers. There is probably an example or exercise in your book that uses pointers to "swap" the values of two variables.

Pointers to arrays, structures, and objects are very common because they give you access to all of the variables & data they contain.