top of page

Pointer in c++


what is pointer?

Pointer is a variable which store address of another variable.

Use of pointer?

Pointers are very useful for another type of parameter passing, usually referred to as Pass By Address. Pointers are essential for dynamic memory allocation. You can also use the pointer to get the value of the variable, by using the * operator (the dereference operator).


#include<iostream>
Using namespace std;
Int main()
{
 Int num =10;
 Int *ptr = NULL;
 Ptr = &num;
 Cout<<*ptr<<endl;
}

Output:10

Types of Pointer?

Null Pointer: A pointer that points to nothing(NULL) is called a Null pointer.

Advantage of null pointer:

· We can initialize a pointer variable when that pointer variable is not assigned any actual memory address.

int *aInt = NULL;

· We can pass a null pointer to a function argument when we are not willing to pass any actual memory address.

int fun(int *ptr)
{ 
      return 15;
}
fun(NULL);

Void Pointer:

A pointer that is not allied with any data types. This points to some data location within the storage means points to that address of variables. It is also known as a general-purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.

int x= 10;
char y= ‘a’;
void *p= &x //void pointer contains address of int x
p = &y //void pointer holds of char y

Wild Pointer:

Pointers that are not initialized are called wild pointers. This pointer may be initialized to a non-NULL garbage value which may not be a valid address.

Remember if a pointer p points to any known variable, then it is not a wild pointer. In the below program p is a wild pointer until it points to x.

int main()
{
int = *p; // wild pointer
int x= 20;
p= &x // p is not a wild pointer now
}

Advantage of Pointer:

  • It allows management of structures which are allocated memory dynamically.

  • It allows passing of arrays and strings to functions more efficiently.

  • It makes possible to pass address of structure instead of entire structure to the functions.

#include <iostream>
using namespace std;
void display(int *ptr,int n)
{
 for(int i = 0;i<n;i++)
 {
 cout<<*ptr<<endl;
 ptr++;
 }
}
int main()
{
 int num[] = {10,20,30,40,50};
 int n = sizeof(num)/sizeof(num[0]);
 display(num,n);
 return 0;
}

What is constant pointer and pointer to constant?

Constant pointer:

A pointer that You cannot change, but can change the value pointed by pointer.

Just like a normal const variable, a const pointer must be initialized to a value upon declaration. This means a const pointer will always point to the same address.

Ex:

Int a =10;

Int b = 20;

Int * const ptr = &a;

Ptr = &b; //invalid

*ptr = 15;

Pointer to constant:

A pointer which can not change the value they are pointing.

Int a = 10;

Int b = 20;

Const int *ptr = &a;

*ptr = 15 //invalid

Ptr = &b;

Function Pointer(pointer to function):

A pointer which is used to point function.

Syntax:

int (*FuncPtr) (int,int);

<returntype><*functionpointername> <type and number of argument passed>

#include<iostream>
Using namespace std;
Void display(int x)
{
 Cout<< x <<endl;
} 
Int add(int x,int y)
{
 Int z = x+y;
 Return z;
}
Int main()
{
 Int a = 10,b =20;
 Int(*funcptr)(int,int);
 Funcptr = add//calling a function using function pointer.
 Funcptr(a,b);
}

Array of function pointer:

#include <stdio.h> 
void add(int a, int b) 
{ 
    printf("Addition is %d\n", a+b); 
} 
void subtract(int a, int b) 
{ 
    printf("Subtraction is %d\n", a-b); 
} 
void multiply(int a, int b) 
{ 
    printf("Multiplication is %d\n", a*b); 
} 
 
int main() 
{ 
    // fun_ptr_arr is an array of function pointers 
    void (*fun_ptr_arr[])(int, int) = {add, subtract, multiply}; 
    unsigned int ch, a = 15, b = 10; 
 
    printf("Enter Choice: 0 for add, 1 for subtract and 2 "
            "for multiply\n"); 
    scanf("%d", &ch); 
 
    if (ch > 2) return 0; 
 
    (*fun_ptr_arr[ch])(a, b); 
 
    return 0; 
}

Use of function pointer:

Passing functions as arguments to other functions:

One of the most useful things to do with function pointers is pass a function as an argument to another function. Functions used as arguments to another function are sometimes called callback functions.

// A simple C program to show function pointers as parameter 
#include <stdio.h> 
// Two simple functions 
void fun1() { printf("Fun1\n"); } 
void fun2() { printf("Fun2\n"); } 
// A function that receives a simple function 
// as parameter and calls the function 
void wrapper(void (*fun)()) 
{ 
 fun(); 
} 
int main() 
{ 
 wrapper(fun1); 
 wrapper(fun2); 
 return 0; 
}

we can use function pointers to avoid code redundancy.

#include <stdio.h> 
void add(int a, int b) 
{ 
     printf("Addition is %d\n", a+b); 
} 
void subtract(int a, int b) 
{ 
     printf("Subtraction is %d\n", a-b); 
} 
void multiply(int a, int b) 
{ 
     printf("Multiplication is %d\n", a*b); 
} 
   
int main() 
{ 
	// fun_ptr_arr is an array of function pointers 
	void (*fun_ptr_arr[])(int, int) = {add, subtract, multiply}; 
	unsigned int ch, a = 15, b = 10;    
	printf("Enter Choice: 0 for add, 1 for subtract and 2 " "for multiply\n"); 
	scanf("%d", &ch); 
   
	if (ch > 2) return 0; 
	(*fun_ptr_arr[ch])(a, b);   
     	return 0; 
 } 

Comments


bottom of page