# Thread: do pointers affect what they point to directly?

1. ## do pointers affect what they point to directly?

if i feed pointers to a function and add 1 to them for example, does it change the actual value of the variable the pointer is pointing to? thanx

2. if you dereference the pointer and add 1 to that, it will affect the value who's address was passed (so it will change the value outside of the function as well). If you just add to the pointer without dereferencing it, it will NOT affect the pointer outside of the function.

3. and i dereference it how? sorry im a bit new to this lingo hehe

4. dereferencing is when you access the value of the data pointer to by a pointer rather than the memory address of the variable pointed to. You usually do that with the unary operator*

int a; // making an int
int* PointerToAnint = &a; // Making a pointer to an int and initializing it to the address of a
*PointerToAnint = 4; // dereferencing the pointer and setting the value of the variable it points to to 4.

5. Dereferencing is working with variables, through pointers. Typically, to manipulate variable values with a pointer, you'd do this:

Code:
```...
int MyAge = 16;
cout << MyAge << endl;
int * pMyAge = &MyAge;
*pMyAge = 21;
cout << MyAge << endl;
...```
That should print out 16, then 21. I used the 'pMyAge' pointer to manipulate the 'MyAge' variable. This is dereferencing. the line '*pMyAge = 21;' means this:

"Assign the value of 21 to the variable that 'pMyAge' is pointing to"

 My explanation > Polymorphic's

6. howdy,
if i feed pointers to a function and add 1 to them for example
a pointer is an address not a variable, if you add 1 to a pointer it will no longer point to the address it originaly pointed to.
pointer handling is a subject in itself.

M.R.

7. ## The short answer to your question is "YES"

When you pass a pointer to a function you can change the original value.

A function can only return one value. So, if you need a function to change two values (say X and Y cooridinates), you can pass-in pointers to both and change both.

Popular pages Recent additions