15 Pointer
15 Pointer
15 Pointer
15.1 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.
15.2 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
15.3 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 ( ) {
Pointer :: 147
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.
The above program can be written as pointer notation. # include < iostream.h > void main ( ) { int A [5] = { 20, 35, 25, 22, 27 } for (int i = 0; i < 5; i + + ) cout << \n << *(A + i); } At one stage the value of i is 2. Therefore A + i is 2, i.e., two locations from the zero location. The *(A+i) will print the data stored at that location.
Pointer :: 149
int rn; }; The statement student st; declares st as the variable of the structure student. The statement student *ptr; declares a pointer variable ptr to a student. That data members using ptr can be referred to as ptr -> name; ptr -> rn; Another way of referring the data member is (*ptr) . name; (*ptr) . rn;
cin >> name; cin >> rn; cin >> marks; } void student : : putdata ( ) { cout << Name << << name << \n; cout << Roll no << rn << \n; cout << Marks << marks << \n; } Declare an object st and pointer ptr as follows: student st; student *ptr; We can refer to the member functions and data member of student in two ways. (i) using dot operator st. marks = 90; st. getdata ( ); (ii) using arrow operator and object pointer ptr -> marks = 90; ptr -> getdata ( ); another way to represent is (*ptr) (*ptr) . marks = 90; (*ptr) . getdata ( );
Pointer :: 151
int rn; public: void getdata ( ) { cin >> this -> rn; } void putdata ( ) { cout << this -> rn; }; void main ( ) { ABC A, B; A . getdata ( ); A . putdata ( ); B . getdata ( ); B . putdata ( ); } When a getdata ( ) or putdata ( ) function is called through object A, this has the address of object A. Similarly, when a getdata ( ) or putdata ( ) function is called through object B, this has the address of object B.
If a pointer ptr points to a variable A, write a statement that represents the content of A but does not use its name. The expression *ptr can be said to
4.
be a pointer to ptr refer to the contents of ptr refer to the value of the variable pointed to by ptr dereference ptr
The expression int* can be said to (i) (ii) (iii) be a pointer pointing to variable int refer to contents of int be a pointer to a int type value.
6.
Fill in the blanks: (a) (b) (c) (d) (e) A pointer variable stores the ............................ of another variable The ....................... operator is called address operator. To declare t_age pointer to integer, we should write ..................... If A is an integer variable than .................... gives its address. If ptr is a pointer to an integer variable, the number stored in it is given by ..............................
7.
State whether the following are True or False. (a) (b) A pointer is a address of the variable. Dereferencing operator (*) is a unary operator. It is different from the multiplication operator (*) which needs two operands. this pointer points to the objects that is currently used to invoke a function.
(c)
Pointer :: 153
2. 3.
What is the difference between arr [4] and *(arr+4)? If a structure defined has pointer variable, how can it access the members of the structure ? Explain if by taking an example. How a data member and member function present in public in class accessed through pointer object? Explain it by taking an example. What is this pointer? Explain briefly?
4.
5.
2. 3. 4. 5. 6.