I'm just starting out with pointers, and I'm slightly confused. I know &
means the address of a variable and that *
can be used in front of a pointer variable to get the value of the object that is pointed to by the pointer. But things work differently when you're working with arrays, strings or when you're calling functions with a pointer copy of a variable. It's difficult to see a pattern of logic inside all of this.
When should I use &
and *
?
Best Answer
You have pointers and values:
You turn a pointer into a value with
*
:You turn a value into a pointer with
&
:Edit: In the case of arrays, they are treated very much like pointers. If you think of them as pointers, you'll be using
*
to get at the values inside of them as explained above, but there is also another, more common way using the[]
operator:To get the second element:
So the
[]
indexing operator is a special form of the*
operator, and it works like this: