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 *
?
Ok, looks like your post got editted...
See how you can use the
&
to get the address of the beginning of the array structure? The followingwill do the same thing.
Yeah that can be quite complicated since the
*
is used for many different purposes in C/C++.If
*
appears in front of an already declared variable/function, it means either that:*
gives access to the value of that variable (if the type of that variable is a pointer type, or overloaded the*
operator).*
has the meaning of the multiply operator, in that case, there has to be another variable to the left of the*
If
*
appears in a variable or function declaration it means that that variable is a pointer:If
&
appears in an variable or function declaration, it generally means that that variable is an reference to an variable of that type.If
&
appears in front of an already declared variable, it returns the address of that variableAdditionally you should know, that when passing an array to a function, you will always have to pass the array size of that array aswell, except when the array is something like a 0-terminated cstring (char array).
Actually, you have it down pat, there's nothing more you need to know :-)
I would just add the following bits:
&
takes a variable and gives you the address,*
takes an address and gives you the variable (or contents).char **p
means thatp
is a pointer to a pointer to achar
.As to things working differently, not really:
\0
) character.I think you are a bit confused. You should read a good tutorial/book on pointers.
This tutorial is very good for starters(clearly explains what
&
and*
are). And yeah don't forget to read the book Pointers in C by Kenneth Reek.The difference between
&
and*
is very clear.Example:
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:Put simply
&
means the address-of, you will see that in placeholders for functions to modify the parameter variable as in C, parameter variables are passed by value, using the ampersand means to pass by reference.*
means the dereference of a pointer variable, meaning to get the value of that pointer variable.The above example illustrates how to call a function
foo
by using pass-by-reference, compare with thisHere's an illustration of using a dereference
The above illustrates how we got the address-of
y
and assigned it to the pointer variablep
. Then we dereferencep
by attaching the*
to the front of it to obtain the value ofp
, i.e.*p
.