Pointers in C

Get Certified in C Programming and Take Your Skills to the Next Level

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;  
}

 

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *