What is a pointer, Advantages and Disadvantages

Share Your Love

In the C/C++ programming language, a pointer is a variable that holds the address of another variable. The type of this variable can be int, char, array, function, or any other pointer. The pointer’s size is determined by the architecture. A pointer in 32-bit architecture, on the other hand, is 2 bytes in size.

Consider the following example to create a pointer that stores an integer’s address.

int x = 10;
int *p = &x; //Variable p of type pointer is pointing to the address of the //variable n of type integer.

How to declare a Pointer?

The asterisk symbol( * ) can be used to declare a pointer in the C language (asterisk symbol). It is sometimes referred to as an indirection pointer because it is used to dereference a pointer.

Here below example:

int *a;//pointer to int  
char *c;//pointer to char 
float *d;//pointer to float

All the types decide by variables to hold an address. Means int pointer stores the address of integer type variable likewise float pointer stores float type variable.

Example Of Pointer:

The following is an example of printing the address and value using pointers.

pointer2
Pointer

Here “a” is an integer type variable while *p is also the same type. So “*p” holds the address of “a” variable in the above picture.

We can display the value of the pointer variable p using the * (indirection operator).

Let’s see an example of the above figure:

#include<stdio.h>  
int main()
{  
	int number = 10;    
	int *p;      
	p = &number;//stores the address of number variable  
  	printf("Address of number variable is %x\n", &x);//&x prints address of number variable
	printf("Address of p variable is %x \n",p); // p contains the address of the number therefore printing p gives the address of number.     
	printf("Value of p variable is %x \n", p); //prints the value at address of p 
    printf("Value of *p variable is %d \n",*p); // As we know that * is used to dereference a pointer therefore if we print *p, we will get the value stored at the address contained by p.    
	return 0;  
} 

Output:

Address of number variable is 1000
Address of p variable is 2000
Value of p variable is 1000
Value of *p variable is 10

Advantages Of Pointer:

  • The pointer is used to retrieve strings, trees, and other objects and is utilised with arrays, structures, and functions to minimise code and enhance speed.
  • Using the pointer, we can return several values from a function.
  • It allows you to access any memory address on the computer’s memory.

Usage of pointer:

In the C programming language, pointers can be used in a variety of ways.

1) Dynamic Memory Allocation:

The pointer is used in the malloc() and calloc() methods in the C language to dynamically allocate memory and also new and delete operator used in C++.

2) Structures, Functions, and Arrays:

In the C/C++ programming language, pointers are commonly utilised in arrays, functions, and structures. It minimises the amount of code and boosts performance.

Disadvantages Of Pointer:

The downsides of using a pointer in C programming are as follows:

  • If pointers are referenced with incorrect values, then it affects the whole program.
  • If dynamically allocated memory is not released, a memory leak develops.
  • An uninitialized pointer can cause a segmentation fault.

Address Of (&) Operator:

The address of a variable is returned by the operator ‘&’. However, to display the address of a variable, we must use % u.

#include<stdio.h>  
int main()
{  
	int a=10;   
	printf("value of a is %d, address of number is %u",a,&a);    
	return 0;  
}  

Output:

Value of a is 10, address of number is 1000

Null Pointer:

The NULL pointer is a pointer that has been assigned no value other than NULL. If there is no address to specify in the pointer at the time of declaration, NULL can be assigned. It will create a more effective strategy.

int *p=NULL

If you want to improve this post please connect with us.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 429

Newsletter Updates

Enter your email address below to subscribe to our newsletter