Pointers
A pointer is a variable,which contains the address of a memory location of another variable,rather than the stored value of that variable.
Why are pointer used?
Few of the situations where pointers can be used are:-
- To return more than one value from a function
- to pass arrays and strings more conveniently from one function to another
- to manupulate arrays easily by moving pointers to them (or to parts of them),instead of moving the array themselves.
- to allocate memory and access it(dynamic memory allocation
Pointers Variable
type *name
Pointers to pointer
A pointer to a pointer is a form of multiple indirection, or a chain of pointers. Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value as shown below and pointer to pointer will declare with double astrick (**).

Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,*p,**p1;
clrscr();
printf("Enter any number=");
scanf("%d",&a);
p=&a;
p1=&p;
printf("%d\n%d\n%u\n%d\n%u",a,*p,p,**p1,p1);
getch();
}
#include<conio.h>
void main()
{
int a,*p,**p1;
clrscr();
printf("Enter any number=");
scanf("%d",&a);
p=&a;
p1=&p;
printf("%d\n%d\n%u\n%d\n%u",a,*p,p,**p1,p1);
getch();
}
Pointers & Arrays
You can also use pointers to access arrays.
Pointers with One Dimensional Array
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,*p;
clrscr();
for(i=0;i<=4;i++)
{
printf("Enter elements=");
scanf("%d",&a[i]);
}
p=a;
for(i=0;i<=4;i++)
{
printf("%d\n",*p);
*p++;
}
getch();
}
#include<conio.h>
void main()
{
int a[5],i,*p;
clrscr();
for(i=0;i<=4;i++)
{
printf("Enter elements=");
scanf("%d",&a[i]);
}
p=a;
for(i=0;i<=4;i++)
{
printf("%d\n",*p);
*p++;
}
getch();
}
Pointers with Multi Dimensional Array
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],r,c,i,*p;
clrscr();
for(r=0;r<=2;r++)
{
for(c=0;c<=2;c++)
{
printf("Enter elements=");
scanf("%d",&a[r][c]);
}
}
p=a;
for(i=0;i<=9;i++)
{
printf("%d\n",*p);
*p++;
}
getch();
}
#include<conio.h>
void main()
{
int a[3][3],r,c,i,*p;
clrscr();
for(r=0;r<=2;r++)
{
for(c=0;c<=2;c++)
{
printf("Enter elements=");
scanf("%d",&a[r][c]);
}
}
p=a;
for(i=0;i<=9;i++)
{
printf("%d\n",*p);
*p++;
}
getch();
}
Allocating Memory
Since C is a structured language, it has some fixed rules for programming. One of them includes changing the size of an array. An array is a collection of items stored at contiguous memory locations.As can be seen, the length (size) of the array above is 9. But what if there is a requirement to change this length (size)? For example,
- If there is a situation where only 5 elements are needed to be entered in this array. In this case, the remaining 4 indices are just wasting memory in this array. So there is a requirement to lessen the length (size) of the array from 9 to 5.
- Take another situation. In this, there is an array of 9 elements with all 9 indices filled. But there is a need to enter 3 more elements in this array. In this case, 3 indices more are required. So the length (size) of the array needs to be changed from 9 to 12.
C malloc() method
The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form. It doesn’t Initialize memory at execution time so that it has initialized each block with the default garbage value initially.
Syntax:-
ptr = (int*) malloc(100 * sizeof(int));
C calloc() method
“calloc” or “contiguous allocation” method in C is used to dynamically allocate the specified number of blocks of memory of the specified type. it is very much similar to malloc() but has two different points and these are:
- It initializes each block with a default value ‘0’.
- It has two parameters or arguments as compare to malloc().
Syntax:-
ptr = (float*) calloc(25, sizeof(float));
C free() method
“free” method in C is used to dynamically de-allocate the memory. The memory allocated using functions malloc() and calloc() is not de-allocated on their own. Hence the free() method is used, whenever the dynamic memory allocation takes place. It helps to reduce wastage of memory by freeing it.
Syntax:-
free(ptr);
C realloc() method
“realloc” or “re-allocation” method in C is used to dynamically change the memory allocation of a previously allocated memory. In other words, if the memory previously allocated with the help of malloc or calloc is insufficient, realloc can be used to dynamically re-allocate memory. re-allocation of memory maintains the already present value and new blocks will be initialized with the default garbage value.
Syntax:-
ptr = realloc(ptr, newSize);
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n,*p;
clrscr();
printf("Enter size to allocate=");
scanf("%d",&n);
//p=(int*)malloc(n*sizeof(int));
p=(int*)calloc(n,sizeof(int));
if(p!=NULL)
{
printf("memory allocated successfully");
}
else
{
printf("Sorry..!!!");
}
p=realloc(p,8);
free(p);
getch();
}
#include<conio.h>
void main()
{
int n,*p;
clrscr();
printf("Enter size to allocate=");
scanf("%d",&n);
//p=(int*)malloc(n*sizeof(int));
p=(int*)calloc(n,sizeof(int));
if(p!=NULL)
{
printf("memory allocated successfully");
}
else
{
printf("Sorry..!!!");
}
p=realloc(p,8);
free(p);
getch();
}