You are on page 1of 17

Sruthi M, ECE

C++ Pointers - 2

Sruthi M.
Assistant Professor, Department of ECE
Vidya Academy of Science and Technology, Thrissur
Ref: Object Oriented Programming using C++ and JAVA, E. Balaguruswamy, Mc Graw Hill
Education
Sruthi M, ECE

Pointer Concepts Contd..


1. Null pointer (Ref. C++ Pointers – 1)
2. Pointer Arithmetic
3. Using Pointers with arrays and Strings
4. Array of Pointers
5. Pointer to pointer
6. Pointer to function
7. Pointer to Objects
8. This Pointer
9. Pointers to derived class
Sruthi M, ECE

Defining Constants in C++


• There are two simple ways in C++ to define constants −
• Using #define preprocessor.
• Using const keyword.

• #define identifier value


– #define LENGTH 10
– #define WIDTH 5
– #define NEWLINE '\n'
• const type variable = value;
– const int LENGTH = 10;
– const int WIDTH = 5;
– const char NEWLINE = '\n';
Sruthi M, ECE

Pointer Arithmetic
• Perform arithmetic operations on a pointer just as a
numeric value
• Four arithmetic operators that can be used on
pointers: ++, --, +, and –
• Example:
– int a[10] ;
– int *ptr;
– ptr = &a[0]; // ptr refers to base address of variable a
– ptr++; or ++ptr // incrementing pointer
– ptr--; or –ptr // decrementing pointer
Sruthi M, ECE

Using Pointers with arrays and Strings


• Pointer is an efficient tool to access elements of an array.
• Declaration –
– int *nptr;
– nptr = number[0]; or nptr = number;
• String is 1-D array of characters, which start with index 0 and
ends with null character “\0”.

• A pointer variable can access a string by referring to its first


character. There are 2 ways to assign a value to a string.
– char num[] = “one”; //creates an array of 4 characters – o, n,e,\0
– const char *numptr = “one”; // generates a pointer variable which points
to first character, o
Sruthi M, ECE

Array of Pointers
• Points to an array of data items.
• Declaration –
• Int *inarray[10]; //declares an array of 10
pointers, each points to an integer.
• Address of first pointer is inarray[0];
• Address of final pointer is inarray[9];
Sruthi M, ECE

Pointer to pointer (Double pointer)


• Pointer holds the address of another variable of
same type.
• When a pointer holds the address of another pointer
then such type of pointer is known as pointer-to-
pointer or double pointer.
• Declaration –
– int **pr;
– Here pr is a double pointer.
– There must be two *’s in the declaration of double
pointer.
Sruthi M, ECE

Sample program
• #include <iostream> • // take the value using pptr
• using namespace std; • cout << "Value of var :" <<
• int main () { var << endl;
• int var; • cout << "Value available at
• int *ptr; *ptr :" << *ptr << endl;
• int **pptr; • cout << "Value available at
• var = 3000; **pptr :" << **pptr << endl;
• // take the address of var
• ptr = &var;
• // take the address of ptr using
• return 0;
address of operator & • }
• pptr = &ptr;
Sruthi M, ECE

Output of Sample program


• Value of var :3000
• Value available at *ptr :3000
• Value available at **pptr :3000
Sruthi M, ECE

Pointer to function (callback function)


• C++ program can select a function dynamically at run
time.
• Can also pass a function as an argument to another
function. Function is passed as a pointer.
• Declaration –
– Int (*function(int x));
– Declaring a pointer only creates a pointer. It does not
create actual function. We must define the task, which is
to be performed by the function.
– Function must have same return type and arguments
Sruthi M, ECE

Pointer to Objects
• A pointer can point to an object created by a class.
• Let item is a class and x is an object defined,
• item x;
• pointer ptr of type item is defined as:
• item *ptr =&x;
• Object pointers are used to access the public members of an object
• 2 methods of referring member functions
– x.getdata(); (using dot operator and object)
– x.show();

– Or

– ptr-> getdata(); (using arrow operator and object pointer)


– ptr-> show();
Sruthi M, ECE

This Pointer
• Every object in C++ has access to its own
address through an important pointer
called this pointer.
• Friend functions do not have a this pointer,
because friends are not members of a class.
Only member functions have a this pointer.
Sruthi M, ECE

Sample Program


#include <iostream>
using namespace std;
• int main(){
• class Demo { • Demo obj;
• private:
• int num; • obj.setMyValues(100,
• char ch;
• public: 'A');


void setMyValues(int num, char ch){
this->num =num;
• obj.displayMyValues();
• this->ch=ch; • return 0;
• }
• void displayMyValues(){ • }
• cout<<num<<endl;
• cout<<ch;
• }
• };
Sruthi M, ECE

Output of Sample program


• 100
• A
Sruthi M, ECE

Pointers to derived class


• Pointers can be used to both base class and
derived class
• If B is base class and D is derived class from B,
then a variable declared as pointer to B can
also be a pointer to D.
Sruthi M, ECE

Sample Program
• #include <iostream> • int main ()
• using namespace std; {
class Base               Base B1;
{               Base *ptr;
              public:               ptr = &B1;
              int x;               ptr->x = 10;
              void display ()               ptr->display();
              {               Derive D1;
                        cout<<”X=”<<x<<endl;               Derive *ptr1;
              }               ptr1 = &D1;
};               ptr1->x = 10;
class Derive: public Base               ptr1->y = 20;
{               ptr1->display ();
              public: }
              int y;
              void display ();
              {
                        cout<<”X=”<<x<<endl;
                        cout<<”Y=”<<y<<endl;
              }
};
Sruthi M, ECE

Output of Sample program


• X= 10
• X = 10
• Y = 20

You might also like