Senior Secondary Course
Learner’s Guide: Computer Science (330)
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;
A pointer variable points to a data type // assign the address of a variable to a
(like int or string) of the same type and is pointer
created with the * operator. Cout<<”Value at ptr”<<ptr<<endl;
Cout<<”Value at var”<<var<<endl;
SYNTAX: datatype *var_name; Cout<<”Value at *ptr”<<*ptr<<endl;
int *ptr;
int main()
HOW TO USE POINTER? : {
o Define a pointer variable pointer();
o Assigning the address of a variable to }
a pointer using unary operator (&) OUTPUT:
which returns the address of that Value at ptr = 0x7ffcb9e9ea4c
variable. Value at var = 20
o Accessing the value stored in the
address using unary operator (*) Value at *ptr = 20
which returns the value of the variable
located at the address specified by its POINTER TO ARRAY:
operand.
o An array name acts like a pointer
constant.
o The value of this pointer constant is
the address of the first element.
o For example, if we have an array
named val then val and &val[0] can
be used interchangeably.
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)
// Declare pointer variable PROGRAM:
int *ptr;
struct point
// Assign address of val[0] to ptr. {
// We can use ptr=&val[0];(both are same)
ptr = val ; int value;
cout << "Elements of the array are: "; };
cout << ptr[0] << " " << ptr[1] << " " <<
ptr[2];
// Driver Code
return;
int main()
}
{
// Driver program struct point s;
int main() struct point *ptr = &s;
{ return 0;
geeks();
}
return 0;
} POINTER TO OBJECT:
OUTPUT:
The next program creates a simple class
Elements of the array are: 5 10 15 called My_Class, defines an object of that
class, called ob, and defines a pointer to an
object of type My_Class, called p.
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
p--; // retreat to previous object
p->show_num(); // again show value of
ob[0]
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
5. Referencing a value through a pointer
is called
A. Direct calling
B. Indirection
C. Pointer referencing
D. All of the above