Computer Science: Pointer: Introduction and Objectives of Pointer
Doorsteptutor material for competitive exams is prepared by world's top subject experts: get questions, notes, tests, video lectures and more- for all subjects of your exam.
Introduction
- In C ++ , the programming with pointers is more powerful and it is used extensively. It saves the processing time.
- Pointer is a variable which holds the address of another variable. So, programming is concerned with the address, not with the direct value stored.
Objectives
After going through this lesson, you would be able to:
- use pointers in arrays
- define pointer variables in a structure and access data members through pointer
- define pointer objects in a class and access members through pointer
Pointer
- A pointer is a variable that represents the location (rather than the value) of a data item such as a variable or an array element.
- Pointers are used frequently in C ++ , as they have a number of useful applications. Consider the following example:
# include < iostream. h >
void main ()
{
int A = 5.
cout ≪ & A.
int ⚹ ptr.
ptr = & A.
cout << ⚹ ptr.
}
- If variable A in the above example has a value 5, then & A holds the address of memory cell A. The variable which holds the address is called pointer variable.
- int ⚹ ptr means that a variable ptr is a pointer to a memory cell which can hold the int data type.
- ⚹ ptr is used for the value stored in a memory cell pointed to by the pointer ptr. It is called de-referencing the pointer.
The output of the above program is the address of memory cell A and value 5.
void ⚹ ptr.
Here ptr can point to any data type.