What is the difference between these two operators? My understanding is that they both point to the memory location of the variable they are used on.
Ex.
int p; foo(*p,&p); &p gets the pointer to the integer p -> That is, the memory address in which p is stored.
*p "dereferences" the pointer, i.e. looks at the object at the memory address provided by p and returns that object.
Your code above is invalid C, as you cannot dereference an int:
error: indirection requires pointer operand ('int' invalid)
Consider instead the following:
// Create an integer int p = 1234; printf("Integer: %d\n", p); // Get the pointer to that integer, i.e. the memory address in which p is stored int *pointer = &p; printf("Pointer: %p\n", pointer); // Dereference the pointer to get the value back int q = *pointer; printf("Dereferenced: %d\n", q); Gives the following output:
Integer: 1234 Pointer: 0x7ffee53fd708 Dereferenced: 1234 Also notice that to print out a pointer address, we have to use a special format specifier %p instead of the %d we'd use for an int.
int p;, the expression *p will not compile.
&preturns the location (address) of the variablep. The expression*preturn the value that is at the location inside thepvariable, often called dereferencing. So ifpcontains the value2020, then*pwill return the integer stored at location 2020 (if possible).*pwill not compile. The operand of unary*must be an expression of pointer type. A pointer cannot contain the value2020(which is of typeint), though it might contain, for example, the value(int*)2020.*must be an expression of pointer type" - or an instance of a class/struct type that implements a memberoperator*.