Unit-2 Pointers
Unit-2 Pointers
24-11-2022 1
UNIT-2
INHERITANCE & POINTERS
where type is any valid C++ data type and var_name is the name of the pointer variable. Following
declarations declares pointers of different types :
•Once a pointer variable has been declared we can use the assignment operator to initialize the
variable.
• Example:
int quantity;
int *p; // declaration
p=&quantity; //initialization.
• Every Variable stored in memory and every memory cell have address associated with it.
• New is used to allocated memory whereas delete is used to0 deallocate the memory.
New operator:
The new operator in C++ is used for dynamic storage allocation. This operator can be used to create
object of any type.
delete operator:
The delete operator in C++ is used for releasing memory space when the object is no longer needed.
Once a new operator is used, it is efficient to use the corresponding delete operator for release of memory.
• A variable that holds an address value is called a pointer variable or simply pointer.
• Pointer can point to objects as well as to simple data types and arrays.
•sometimes we don’t know, at the time that we write the program , how many objects we want to
create . when this is the case we can use new to create objects while the program is running. new
returns a pointer to an unnamed objects
• In C++ programming, this is a keyword that refers to the current instance of the class. There can be
3 main usage of this keyword in C++.
• Pointers are the variables that hold address. Not only can pointers store address of single variable, it
can also store address of an array.
• Example:
int *ptr;
int a[5];
ptr=& a[5];//&a[2] is the address of third element of a[5].
• We can also declare point to whole array instead of only one element of the array.
• Syntax:
data_type(*var_name)[size of array];
Example:
int (*ptr)[10];
• In pointer to pointer , first pointer contains address of second pointer , second pointer contains
address of actual value.
Syntax:
data_type **p;
Syntax:
Output:
Addition:40
Multiplication:15
• We can declare pointer to point to the base class as well as derived class.
C++ Allows you to pass pointer to function . If a pointer passed to a function as a parameter to be
modified then changes made to the pointer does not reflect back outside that outside function.
• The NULL pointer is a constant with a value of zero defined in several standard libraries,
including iostream. Consider the following program −
#include <iostream>
using namespace std; int main ()
{ int *ptr = NULL;
cout << "The value of ptr is " << ptr ;
return 0;
}
Output:
The value of ptr is 0
UNIT-II Inheritance & Pointer
24-11-2022 45
Department Of Computer Engineering
3 Pointer.
3.12 : Null pointer:
• This is done at the time of variable declaration. A pointer that is assigned NULL is called
a null pointer.
• The NULL pointer is a constant with a value of zero defined in several standard libraries,
including iostream. Consider the following program −
#include <iostream>
using namespace std; int main ()
{ int *ptr = NULL;
cout << "The value of ptr is " << ptr ;
return 0;
}
Output:
The value of ptr is 0
UNIT-II Inheritance & Pointer
24-11-2022 46
Department Of Computer Engineering
3 Pointer.
3.13 : Void pointer:
• A void pointer is a pointer that has no associated data type with it. A void pointer can hold address
of any type and can be type casted to any type.
• In C++, we cannot assign the address of a variable to the variable of a different data type.
Example:
int a = 10;
char b = 'x';
void *p = &a; // void pointer holds address of int 'a'
p = &b; // void pointer holds address of char 'b’