Pointer-:


pointer is a special variable that is designed to hold the address of any variable that is eclared n ormally .
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 unsi gned 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 argume nts ,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-:


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 vey ef ficiently.



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 trans ferred to a pointer in two of the following ways .
-:p=&a[0];
-: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 point ers-:


#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();
}