int a = 10;
int* b = &a;
int c = 4;
int* d = &c;
(*d)++;
d = b;
*d = c - *b;
cout << a << " " << c;

Output
5 -5

I am having difficulties understanding pointers, like I know that *something means the value its pointing at, and &something means its the address, but is it possible for someone to walk me through this question as I believe it will clear all my doubts and help me understand better.

4
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by sahmedsa

> (*d)++;
This means increment whatever d is pointing to.
Since it seems to be pointing at c, it's going to increment c.

> cout << a << " " << c;
There's nothing stopping you pasting this statement between every other line to see how things change.

ok so basically it means that
*b=&a
b=10
?

ok so basically it means that
*b=&a
b=10
?

I suppose

int a = 10;
a is an integer with value 10

int* b = &a;
b is a pointer to an integer which points to the address of a

int c = 4;
c is an integer with value 4

int* d = &c;
d is a pointer to an integer which points to the address of c

(*d)++;
d is dereferenced which has the value of c which is 4 and ++ will make it value of 5

if a pointer is pointing to an address putting * to a pointer will "dereference" it meaning getting the value.

d = b;
d is equal to b meaning d points to the same address as b

*d = c - *b;
d derefrenced value is equal to c = 5 - derefrenced b = 10 = -5

remember d is pointing to same address as b which points to a so whatever changed to d
will have effect on a so thats why cout << a has -5

and c is = 5

cout << a << " " << c;

Output
5 -5

O ok, that makes sense. Thank you for your help, really appreciate it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.