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