Site icon DataFlair

Pointers in C

Program 1

// Implementation of pointer and pointer to pointer
#include<stdio.h>
#include<conio.h>
int main()
{
       int a=50,*p;
       system("cls");
    //    printf("Value is: %d",a);
    //    printf("\nAddress is: %u",&a);
       p=&a;
       printf("a=%d *p=%d  ",a,*p);
         ++*p;
         ++a;
         printf("a=%d *p=%d  ",a,*p);

return 0;
}

Program 2

// Implementation of pointer and pointer to pointer
#include<stdio.h>
#include<conio.h>
int main()
{
       int a=50,*p,**r;
       system("cls");
      p=&a;  //pointer
      r=&p;  //pointer to pointer
      
      printf("a= %d",a);       //  50
      printf("\np= %u",p);    // address of p
      printf("\n*p= %d",*p); // 50 
      printf("\nr= %u",r);    // address of r
      printf("\n*u= %u",*r);  // address of p
      printf("\n**r= %d",**r);  // 50
       ++a;
       ++*p;
       ++**r;
       printf("\na= %d",a);       
       printf("\na= %d",*p);       
       printf("\na= %d",**r);       
return 0;
}

Program 3

// Implementation of swaping using call by reference
#include<stdio.h>
#include<conio.h>
void swap( int *p,int *r);
int main()
{
        int x,y;
        system("cls");
        printf("\nEnter two number");
        scanf("%d %d",&x,&y);
        printf("\n Before swaping %d %d",x,y);
        swap(&x,&y);  // Call by reference 
        printf("\n After swaping %d %d",x,y);
        return 0;
}
void swap( int *p,int *r)
{
    int c;
    c=*p;
    *p=*r;
    *r=c;
}

Program 4

// Implementation of swaping using call by reference
#include<stdio.h>
#include<conio.h>
int main()
{
      int *a;
    system("cls");
    printf("%d",sizeof(a));

     return 0;  
}

 

Exit mobile version