Pointer-:

Pointer is a special variable that is designed to hold the address of any variable that is declared normally.

Declaration of a pointer-:


Syntax-:
int *p;
Here, * identifies that p is a pointer
p is name of pointer variable and,int is the data type of that variable whose va lue will be stored in the pointer.

Initialization of a pointer-:


int a,*x;
x=&a;
Address of a goes to x.

Displaying address via pointer-:


int a*p;
p=&a;
printf("%u",p);
Note-:The default type of an address is always unsigned integer.

Displaying the value of variable via pointer.


int a*p;
p=&a;
printf("%d",p);

Call by value.


Calling a function via the value of a variable or simply the variable is called call by value in thi s technique the actual arguments and formal arguments, both are ordinary variables.

Call by reference.


Calling a function by the address of variable is c alled call by reference.here ,the actual argument is address of variable while formal argument is a pointer.

Call by value.


Syntax-:
void main()
{
int a,b,c;
__________
__________
c=function(a,b);
__________
__________
__________
}
int function(int x,inty)
{
__________
__________
__________
return z;
}


Call by reference.


Syntax-:
void main()
{
int a,b,c;
__________
__________
c=function(&a,&b);
__________
__________
__________
}
int function(int *p1,int*p2)
{
__________
__________
__________
return *p3;
}

Pointers with arrays.


Since,an array is designed to hold multiple elemen t of same type ,all elements are stored on coniguo us memory locations . in this kind of contiguous address allocation a po inter can fetch every element of that array very efficiently.

Memory allocation in array.


~~~~~~~~~


1120______________a[0]
1122______________a[1]
1124______________a[2]
1126______________a[3]
1128______________a[4]


~~~~~~~~~~



int *p1;
p1=&a[0];
p1=p1+1;
p1=p1+1;
p1=p1+1;
p1=p1+1;

The address of first element of array can be transferred to a pointer in two of the following ways.

  1. p=&a[0];
  2. p=a;


Program to add number via pointer.


#include< stdio.h>
#include< conio.h>
void main()
{
int a ,b,c,d,*p1,*p2,*p3,*p4;
clrscr();
printf("enter three values");
scanf("%d%d%d",&a,&b&c);
p1=&a;
p2=&b;
p3=&c;
p4=&d;
*p4=*p1+*p2+*p3;
printf("the sum of both is %d",*p3);
getch();
}

Program that inputs colour of traffic light and di splays whether to stop ,or to go or to to get read y via pointer.


#include< stdio.h>
#include< conio.h>
void main()
{
char ch,*p1;
printf("enter the first letter red ,yellow or green ");
scanf("%c",&ch);
p1=&ch;
switch(*p1)
{
case r:
case R:
printf("stop");
break;
case g:
case G:
printf("go");
break;
case y:
case Y:
printf("get ready");
break;
default:
printf("invalid choice");
break;
}
}

Program to add two numbers via functions with pointers.


#include< stdio.h>
#include< conio.h>
int addition(int*,int*);
void main()
{
int a,b,sum;
int addition(int,int)
void main()
{
sum=addition(&a,&b);
printf("the sum is %d",sum);
getch();
}
int addition(int*p1,int*p2)
{
int s,*p3;
p3=&z;
*p3=*p1+*p2;
return *p3;
}

Program to demonstrate the use of pointer in an array.


#include< stdio.h>
#include< conio.h>
void main()
{
int a[15],*p,i;
clrscr();
p=&a[0];
printf("enter five elements");
for(i=0;i<5;i++)
{
scanf("%d",p);
p++;
}
printf("following are the elements you entered ");< br> p=&a[0];
for(i=0;i<5;i++)
{
printf("%d was stored at %u",*p,p);
p++;
printf("\n");
}
getch();
}

Calculating area of circle using pointer.


#include< stdio.h>
#include< conio.h>
#define pi 3.14
void main()
{
float area,*p1;
int r,*p2;
printf("enter the radius of circle");
scanf("%d",&r);
*p2=&r;
*p1=&area;
*p1=pi*p2*p2;
printf("the area is %f",*p1);
getch();
}

Swap two numbers via pointers.


#include< stdio.h>
#include< conio.h>
void main()
{
int a,b,c,*p1,*p2,*p3;
printf("enter the first number");
scanf("%d",&a);
printf("enter the second number");
scanf("%d",&b);
p1=&a;
p2=&b;
p3=&c;
*p3=*p2;
*p2=*p1;
*p1=*p3;
printf("the first number after swapping is %d",*p1) ;
printf("the second number after swapping is %d",*p2 );
getch();
}

Program to change the case of entered character vi a pointer.


#include< stdio.h>
#include< conio.h>
void main()
{
char ch,*p1;
clrscr();
printf("enter the character");
scanf("%c",&ch);
p1=&ch;
if(*p1>=65&&*p1<=90)
{
*p1=*p1+32;
}
else if(*p1>=97&&*p1<=122)
{
*p1=*p1-32;
}
printf("%c",*p1);
getch();
}

Program to find maximum in the array.


#include< stdio.h>
#include< conio.h>
void main()
{
int a[5],i,max,*p;
clrscr();
printf("enter 5 elements");
p=&a[0];
for(i=0;i<5;i++)
{
scanf("%d",p+i);
}
max=a[0];
for(i=0;i<5;i++)
{
if((*p+i)>max)
{
max=(*p+i);
}
}
printf("the biggest element in the array was %d",ma x);
getch();
}

Sorting of array via pointer.


#include< stdio.h>
#include< conio.h>
void main()
{
int i,j,*p,t,a[5];
clrscr();
printf("enter 5 elements ");
p=&a[0];
for(i=0;i<5;i++)
{
scanf("%d",p+i)
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(*(p+j)>*(p+i))
{
t=*(p+j);
*(p+j)=*(p+i);
*(p+i)=t;
}
}
}
printf("the sorted array is \n");
for(i=0;i<5;i++)
{
printf("%d",*(p+i));
}
getch();
}

Copying a string via pointer.


#include< stdio.h>
#include< conio.h>
void main()
{
char a[5],b[5],*p1,*p2;
int i;
clrscr();
printf("enter a string");
scanf("%s",a);
p1=&a[0];
p2=&b[0];
for(i=0;i<5;i++)
{
*(p2+i)=*(p1+i);
}
printf("the copied string is%s",b);
getch();
}

Program to reverse the elements of array via pointer.


#include< stdio.h>
#include< conio.h>
void main()
{
int *p,a[5],i;
p=&a[0];
clrscr();
for(i=0;i<5;i++)
{
printf("enter the elements of array");
scanf("%d",p+i);
}
p=&a[4];
for(i=4;i>=0;i--)
{
printf("%d",*p);
p--;
printf("\n");
}
getch();
}

Program to delete a character of string via pointer.


#include< stdio.h>
#include< conio.h>
void main()
{
int pos,x,i,*p2;
clrscr();
printf("enter the string");
scanf("%s",a);
p1=a;
printf("enter the string position ");
scanf("%d",&pos);
p2=&pos;
x=strlen(p1);
for(i=*p2-1;i {
a[i]=a[i+1];
a[i]='\0';
printf("%s",a);
getch();
}