# Thread: Do I understand pointers correctly?

1. ## Do I understand pointers correctly?

Ok, I'm trying to see if I understand points correctly. Here is how I understand them:

1) Pointers act as a variable that points to the memory adress of another variable:
Code:
```
int a = 10;
int *b= &a;```
2) When dereferenced, you can output the value of the variable the pointer references:
Code:
```std::cout<<*b<<endl;
OUTPUT: 10```
3) When not dereferenced, you can output the memory adress of the variable the point references. Which can later be used to read the memory of a variable from another application (such as in a minesweeper cheat):
Code:
```std::cout<<b<<endl;
OUTPUT: *bunch of hex*```
4) Pointers can be used to pass by reference, so that a variable can be modified by a function without having to return it - allowing for the modification of multiple variable within a function:
Code:
```
int inc(int a, int *b)
{
a++:
*b = 20;//Is this legal?
return a;
}
int f = 0;
int g = 1;
inc(f, &g)//f is now 1, g is now 20```

Ok, that's all I can think of right now. However, I want to know if I am right on those four points, and if there is anything else I should know.

EDIT: Yeah yeah, this is the 5 trillionth post on pointers, I know. However, unless I ask myself I won't be sure

2. Yes
Yes
Yes and No - reading another processes memory is far more involved than just declaring a pointer.
Yes.

3. >> inc(f, &g)//f is now 1, g is now 20
The comment is incorrect. After that call f is still 0 because it is passed by value to the inc function. It would work if you had:
Code:
`f = inc(f, &g)//f is now 1, g is now 20`
It would also work if a was a reference parameter instead.

4. Originally Posted by Salem
Yes and No - reading another processes memory is far more involved
Yeah, I understand that it needs more than just a pointer - I was just using that as an example of when one might be used

Ok, thanks Salem! Now I can rest easily

EDIT:
Originally Posted by Daved
>> inc(f, &g)//f is now 1, g is now 20
The comment is incorrect. After that call f is still 0 because it is passed by value to the inc function. It would work if you had:
Code:
`f = inc(f, &g)//f is now 1, g is now 20`
It would also work if a was a reference parameter instead.
Ok, I thought I needed to do that, but decided not to for reasons I no longer remember.

5. 1) Pointers act as a variable that points to the memory adress of another variable:
A variable that is a pointer type stores a value just like any other variable. The value it stores is the address of another variable.

2) When dereferenced, you can output the value of the variable the pointer references:
When you dereference a pointer variable, you are using the address stored in the pointer variable to access the value at that address in memory.

3) When not dereferenced, you can output the memory adress of the variable the point references.
Like most variables, you can output the value stored in the pointer, which is an address(although for pointers to type char it is trickier).

6. 1) Pointers act as a variable that points to the memory adress of another variable:
Code:

int a = 10;
int *b= &a;
Pointers act as a variable that points to a memory adress that may be another variable.

Code:
`WIKI *widget = new Pointer_Wiki_Widget();`
in this case the WIKI type pointer called widget points to an objects adress in memory. Is this really a variable?

7. Originally Posted by curlious
Pointers act as a variable that points to a memory adress that may be another variable.

Code:
`WIKI *widget = new Pointer_Wiki_Widget();`
in this case the WIKI type pointer called widget points to an objects adress in memory. Is this really a variable?
That's the point I was forgetting to add.

At any rate, I knew that so as far as I'm concerned, I'm good to go unless someone wants to point out anything else I forgot/didn't know.