Originally Posted by
A34Chris
In this case it is the new memory location to point at? *pointer means memory address to point to and &pointer means value stored at that location if my memory is correct.
I think your memory is failing you here. There's a few different aspects of * and & when working with pointers. First, let's start with defitions
& is the address-of operator. It gives you the address of the object on it's right. It only makes sense to apply this to a variable or function, which have actual addresses in memory. If you use & on a variable of type foo, the result is of type "pointer to foo".
* is the dereference operator. It gives you the contents of the memory location on it's right. If you use this on a variable of type "pointer to foo", the result is of type "foo".
* is also use in declarations to signify that variable is a pointer to the aforementioned type.
Declarations
Code:
// a and c are pointers to int, b is a plain int -- notice that the * applies only to an individual variable, not the whole line.
// It doesn't make sense to use & in a declaration.
int *a, b, *c;
// a is a pointer, pointing to (storing the address of) b
a = &b;
// we store 42 in the memory area pointed to by a -- i.e. we store 42 in b
*a = 42;
// this makes no sense and is not valid C -- you can't dereference something that isn't a pointer
*b = 42;
// c doesn't point anywhere, so you're trying to write (assign) to invalid memory -- this is undefined behavior
*c = b;
// c is of type pointer to int; so it a, thus &a is of type pointer to pointer to int -- those types are not compatible
c = &a;
// again, c and a are of type pointer to int; *a is of type int -- those types are also not compatible
c = *a;
EDIT:
It has nothing to do with pointers or the like, but & is also the bitwise AND operator and * is also the multiplication operator. However, in these contexts, they are binary operators, requiring two operands (x & y; v *w). In the pointer context, they are unary operators, having only one operand, which is on their right.