Pointer operations
suggest changeThere are two operators for pointers. The address-of operator (&) returns the memory address of its operand. The dereference operator (*) returns an lvalue for the object at the address given by its operand.
int var = 20;
int *ptr;
ptr = &var;
cout << var << endl;
// Outputs 20 (The value of var)
cout << ptr << endl;
// Prints an address, for example 0x234f119 (var's memory location)
cout << *ptr << endl;
// Outputs 20 (the value of the object ptr points at)
The asterisk (*) is used in declaring a pointer for the simple purpose of indicating that it is a pointer. Don’t confuse this with the dereference operator, which is used to obtain the object located at the specified address. They are two different things represented with the same sign.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents