Pointers
Pointers
20
POINTERS
int *ptr;
POINTERS: A pointer however, is a // note that data type of ptr and var must
variable that stores the memory address as its be same
value. ptr = &var;
PROGRAM:
PROGRAM: using namespace std;
#include <iostream.h>
void pointer() void geeks()
{ {
int var = 20; // Declare an array
int val[3] = { 5, 10, 15};
// declare pointer variable
1
Senior Secondary Course
Learner’s Guide: Computer Science (330)
PROGRAM:
#include <iostream>
using namespace std;
POINTER TO STRING CONSTANT:
class My_Class {
# include < iostream.h > int num;
void main ( ) public:
{ void set_num(int val) {num = val;}
char stu1 [ ] = “work as an array”; void show_num();
char *stu2 = “work as a pointer”; };
cout << stu1; //display work as an array
cout << stu2; // display work as a pointer void My_Class::show_num()
stu1 ++; // wrong statement {
stu2 ++; cout << num << "\n";
cout << stu2; // it prints “ork as a pointer” }
}
int main()
STRUCTURE POINTER: It is defined {
as the pointer which points to the address My_Class ob, *p; // declare an object and
of the memory block that stores pointer to it
a structure is known as the structure
pointer. ob.set_num(1); // access ob directly
2
Senior Secondary Course
Learner’s Guide: Computer Science (330)
ob.show_num();
OUTPUT:
p = &ob; // assign p the address of ob 10
p->show_num(); // access ob using pointer 20
30
return 0;
} this POINTER:
C++ uses a unique keyword called this to
represent the object that invokes a member
function.
// Incrementing and decrementing an object
pointer. This is a pointer that points to the object for
which this function was called.
#include <iostream>
using namespace std; PROGRAM:
class ABC
class My_Class { {
int num; int rn;
public: public:
void set_num(int val) {num = val;} void getdata ( )
void show_num(); {
}; cin >> this -> rn;
}
void My_Class::show_num() void putdata ( )
{ { cout << this -> rn;
cout << num << "\n"; };
} void main ( )
{
int main() ABC A, B;
{ A . getdata ( );
My_Class ob[2], *p; A . putdata ( );
B . getdata ( );
ob[0].set_num(10); // access objects directly B . putdata ( );
ob[1].set_num(20); }
When a getdata ( ) or putdata ( ) function is
p = &ob[0]; // obtain pointer to first element called through object A, this has the address
p->show_num(); // show value of ob[0] of object A. Similarly, when a getdata ( ) or
using pointer putdata ( ) function is called through object B,
this has the address of object B.
p++; // advance to next object
p->show_num(); // show value of ob[1]
using pointer
return 0; }
3
Senior Secondary Course
Learner’s Guide: Computer Science (330)
STRETCH YOURSELF
CHECK YOURSELF
1. What is a pointer and give a suitable
example describing use of it?
1. Which of the following is the correct 2. Give an example of array of pointers.
way to declare a pointer ? 3. Define this pointer. Give an example of
this pointer.
A. int *ptr
B. int ptr
C. int &ptr
D. All of the above ANSWERS
2. A pointer can be initialized with
A. Null
B. Zero Answers to Check Yourself:
C. Address of an object of same type 1. A
D. All of the above 2. D
3. A
3. The operator used for dereferencing or 4. A
indirection 5. B
is ____
A) *
B) &
C) ->
D) –>>
4. Choose the right option:
String *x,y;
A) X is a pointer to a string, y is a
string
B) Y is a pointer to a string, x is a
string
C) Both x and y are pointers to string
types
D) Y is a pointer to a string
A. Direct calling
B. Indirection
C. Pointer referencing
D. All of the above