I was watching a lecture and got confused at a point when professor said
that ptr=&x
denotes a variable ptr
assigned the address of the variable x
.
And for y=*ptr+1
he said *ptr
denotes the value stored at x
(or the value of x
).
I became slightly confused here as *ptr
should be pointing towards the address of x
right, not the value stored at x
? Can someone please elaborate it a bit more?

- 104,111
- 38
- 209
- 254

- 65
- 2
- 3
- 7
-
2`*ptr` refers to the entry stored at `ptr`. See here for an explanation http://en.wikipedia.org/wiki/Pointer_%28computer_programming%29#C_pointers – A.Schulz May 17 '13 at 17:01
-
4`ptr` refers to the address of the variable `x`. `*ptr` refers to the value in memory at the address of `x`. `*ptr = *(&x) = x`. – Patrick87 May 17 '13 at 17:23
-
This is a question about a the behavior concrete piece of C code, not about the semantics of the C language. It is a programming question and not a computer science question, so I am migrating it to [so]. – Gilles 'SO- stop being evil' May 17 '13 at 18:38
-
1You must see [this](http://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean) – Suvarna Pattayil May 17 '13 at 18:44
5 Answers
Consider,
int a = 10;
Now, in memory we have something like
+------+
| |
| 10 |
| |
+------+
0x121 a
Now, consider a pointer variable of type int
int* ap = &a;
This looks like,
+-------+
| |
| 10 |
| |
0x121 +-------+
a
+-------+
| |
| 0x121 |
| |
+-------+
ap
a
is a label to the memory location and ap
is the address. To get the value at that address you use *
. This is called dereferencing the pointer.
*ap
This gives you 10
Read some good tutorial on pointer.

- 5,136
- 5
- 32
- 59
It is ptr
that points to x
, not *ptr
. *ptr
is not even a pointer (assuming x
isn't one).
The variable ptr
contains a pointer to the variable x
, i.e. the address of the variable x
. The value of the expression ptr
is a pointer to x
. The value of the expression *ptr
is the value at the location that ptr
points to: that's what the dereference operator *
means. Since ptr
points to x
, the value of *ptr
is the value of x
.

- 104,111
- 38
- 209
- 254
A pointer points to an address where a value is stored.
int *ptr;
int x = 2;
ptr = &x;
Here, ptr is an int pointer and x is an int (obviously). If we want ptr to "keep track" of the value of x then we assign ptr the address of x. So when we dereference ptr we get the value stored at the address that ptr points to. So if we want to change the value that ptr "stores" then we dereference it.
*ptr = 5;
This changes the value at the address ptr points to from 2 to 5.

- 1,467
- 4
- 20
- 32
Given:
int x = 42;
int *ptr = &x;
x
is an integer object (of type int
), and ptr
is a pointer object (of type int*
or pointer-to-int
).
Unary &
is the address operator. Applying it to an object of type FOO
gives you the address of that object (or, equivalently, a pointer to that object); that address/pointer value is of type FOO*
, or pointer-to-FOO
. The operand of unary &
must be the name of an object, not just a value; &42
is illegal nonsense. (The symbol &
is also used for the binary bitwise and operator, which is completely unrelated to the address operator.)
Unary *
is the dereference operator, the inverse of &
. Its operand must be value of some pointer type. *ptr
refers to the object to which ptr
points.
Given the above declarations, and assuming the value of ptr
hasn't been changed, the expressions x
and *ptr
mean the same thing; they both refer to the same int
object (whose value happens to be 42). Similarly, the expressions &x
and ptr
mean the same thing; they both yield the address of x
, an address that has been stored in the pointer object ptr
.
It's important to note that *ptr
doesn't just refer to the current value of x
, it refers to the object x
itself -- just like the name x
does. If you use *ptr
in a value context, this doesn't matter; you'll just get the value of x
. But if you use it on the left side of an assignment, for example, it doesn't evaluate to 42
. It evaluates to the object x
itself, and lets you modify that object. (The distinction here is whether *ptr
is used as an lvalue.)

- 254,901
- 44
- 429
- 631
The variable ptr
stores the address of x
. To retrieve the value stored at x
, we dereference ptr
with the unary *
operator; hence, the expression *ptr
evaluates to the value of x
.
Put another way, if
p == &x;
then
*p == x;

- 119,563
- 19
- 122
- 198