Pointers in C

The pointer in C language is a variable which stores the address of another variable. This variable can be of type int, char, array, function, or any other pointer. The size of the pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte.
  • Usage of pointer
There are many applications of pointers in c language.

1) Dynamic memory allocation

In c language, we can dynamically allocate memory using malloc() and calloc() functions where the pointer is used.

2) Arrays, Functions, and Structures

  • Pointer Program to swap two numbers without using the 3rd variable
#include<stdio.h>  
int main(){  
int a=10,b=20,*p1=&a,*p2=&b;  
  
printf("Before swap: *p1=%d *p2=%d",*p1,*p2);  
*p1=*p1+*p2;  
*p2=*p1-*p2;  
*p1=*p1-*p2;  
printf("\nAfter swap: *p1=%d *p2=%d",*p1,*p2);  
  
return 0;  
}  
  • Types of Pointers
Null pointer.
Void pointer.
Wild pointer.
Dangling pointer.
Complex pointer.
Near pointer.
Far pointer.
Huge pointer.

**Advantages of Using Pointers

1.Less time in program execution.
2.Working on the original variable.
3.With the help of pointers, we can create data structures (linked-list, stack, queue).
4.Returning more than one values from functions.
5.Searching and sorting large data very easily.
6.Dynamically memory allocation.

Posted on by