You have a typo there. It should be:
int x = 5;
int *k = &x;
It should be read as: k
points to x
. Or if you insist on the "represent" word: *k
represents x
.
The &
operator takes any variable as argument and returns its address (pointer). The *
gets a pointer (address) as argument and returns the value stored there. As such they are opposite operations: &*k
is the same as k
and likewise *&x
is just like x
. You can look at these expression this way:
x //an integer
k //pointer to integer
&x //pointer to x
*k //integer pointed to by k
*&x //integer pointed to by a pointer to x, that is x
&*k //pointer to the integer pointed to by k, that is k
Note that operator &
can only be used on variables, as expressions in general do not have addresses, only variables do (well, and temporaries, but that is another matter). For example, this expression is invalid:
&(x + 1) //error!
The funniest thing with these operator is how a pointer is declared:
int *k;
You might think that it would be better written as int &k
, but that's not how declarators are read in C++. Instead, that declaration should be read as:
int (*k);
that is, *k
is an integer, so it results that k
is a pointer to an integer.
Now, the declaration with initialization is weirder:
int *k = &x;
should actually be read as:
int (*k);
k = &x;
That is, you declare *k
as being an integer, thus, k
is a pointer-to-integer. And then you initialize k
(the pointer) with the address of x
.
Actually you can create pointers of any type, even pointers to pointers, pointers to pointers to pointers... but note that this syntax is illegal:
int x;
int **p = &&x; //error, &x is not a variable
but this is valid:
int x;
int *k = &x;
int **p = &k;