C++ tutorial-C++ Pointer
What is a pointer? A pointer is a variable that holds a memory address and points to the value at the address.
Declaring and using pointers To declare a pointer, you must write down the data type of the pointer and * in front of the name of the pointer. float *i; To hold a memory address of another variable, you will use & operator. Example: float x;
i = &x;
You will use the * operator to dereference a pointer.
Example: float x;
i = &x; *i=10;//assign a value to x variable using pointer i cout<<x;
Operations on a pointer
You can also perform operations on a pointer by using the arithmetic operators such as +, -, *, and /.
cout<<(*p)+10; //add 10 to the value of x variable
cout<<p+1;//add 1 to the address of x variable
A Pointer to an array
A pointer can be used to point an array. The pointer will point to the address of the first element of the array. If you don't know the array please read this page: C++ Array.
To point to the subsequent elements of the array you need to increase the address that the pointer points to by one.
An array of pointers
You can create an array to store a collection of pointers. This is called the array of pointers.