Pointers are symbolic representation of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. It’s general declaration in C/C++ has the format:
int *ptr; //ptr can point to an address which holds int data
Here is a visual example of how pointers work:
How to use a pointer?
- Define a pointer variable – you must use an asterisk (*) when defining a pointer variable. All three of the following are valid – they are the same:
- int *ptr;
- int * ptr;
- int* ptr;
- Assigning the address of a variable to a pointer using unary operator (&) which returns the address of that variable.
- Accessing the value stored in the address using unary operator (*) which returns the value of the variable located at the address specified by its operand.
"Pointers in C/C++ with Examples" by Abhirav Kariya, Geeks for Geeks is licensed under CC BY 4.0