Pointer in C Language

 INTRODUCTION

Pointer is used to store the address of any other variable of similar data type. 

                                                eg. 1) int a=10;         (int *ptr)     

                                                     2) float b=10;      (float *ptr)              

                                                     3)char c='z'          (char *ptr)

we can use different data types in one program but for that we have to declare that data type with ptr.  

                                                prt                 num      

                                               2000                 20     

                                              (2004)              (2000)

num is variable. 20 is the number which is stored in num. And the 2000 is the address of num. ptr stores the address of num. 2004 is the address of ptr. 

NOTE : Data type of variable and pointer should be same.

-Pattern to store the ptr:

VALID : ptr=#   

              *ptr=num;

INVALID : *ptr=num;         

                     ptr=num;

& =for address

* = value which is stored in that address.  

eg. *ptr=num                 .......................................... 20 (value)  

     &ptr=num                 .......................................... 2000(add of value)


Example : 

1) POINTER ARRAY :

code :

#include<stdio.h>

int arr[]={1,2,3,4,5};

int *p[5];

int i;

int main()

{

for(i=0;i<5;i++)

{

p[i]=&arr[i];

printf("address :%d \n",p[i]);

}

for(i=0;i<5;i++)

         {

p[i]=&arr[i];

printf("value :%d \n",*p[i]);

}

}

output :

address :4206608

address :4206612

address :4206616

address :4206620

address :4206624

value :1

value :2

value :3

value :4

value :5


2) POINTER FUNCTION :

code:

#include<stdio.h>

void add(int *sal,int *bon,int *sum);

int main()

{

int sal=90,bon=10,sum=0;

add(&sal,&bon,&sum);

printf("addition is :%d %d\n",sal,bon);

}

void add(int *sal,int *bon,int *sum)

{

    *sum=(*sal)+(*bon);

printf("addition is :%d %d \n",*sum);

}


output :
addition is :100 6487572
addition is :90 10


3) SWAPPING :
 
code:
 
#include<stdio.h>
int swap(int a,int b,int x);
int swap2(int *a,int *b,int *x);
int main()
{
int a=50,b=60,x;
swap(a,b,x);
printf("swapping of :%d %d\n",a,b);
swap2(&a,&b,&x);
printf("swapping is :%d %d\n",a,b);
}

int swap(int a,int b,int x)
{
x=a;
a=b;
b=x;
printf("swapping is :%d %d \n",a,b);
}
int swap2(int *a,int *b,int *x)
{
*x=*a;
*a=*b;
*b=*x;
printf("swapping is :%d %d \n",*a,*b);
}

output :
swapping is :60 50
swapping of :50 60
swapping is :60 50
swapping is :60 50


4) POINTER TO POINTER :

code :

#include<stdio.h>

int main()

{

float n=30;

float*ptr1;

float**ptr2;

ptr1=&n;

ptr2=&ptr1;

printf("value :%.2f \n",n);

printf("value :%.2f \n",*ptr1);

printf("value :%.2f \n",**ptr2);

printf("address :%d \n",&n);

printf("address :%d \n",ptr1);

printf("address :%d \n",*ptr2);

printf("address :%d \n",&ptr1);

printf("address :%d \n",ptr2);

}

output :

value :30.00

value :30.00

value :30.00

address :6487572

address :6487572

address :6487572

address :6487560

address :6487560







   

                       


No comments:

Post a Comment