Site icon DataFlair

C++ Pointers

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

/*
    Pointers are variable which hold address of another variable.
    its also called reference variable in cpp
*/
// Pointers in CPP
#include<iostream>
using namespace std;
int main()
{
    system("cls");
     int a=10,b=5,*p,*r;
     p=&a; 
     r=&b;
     ++*p; 
     --*r;
     r=&a;
     ++*p;
     ++*r;
     ++a;
     ++b;
     cout<<endl<<"a="<<a;
     cout<<endl<<"b="<<b;
     cout<<endl<<"*p="<<*p;
     cout<<endl<<"*r="<<*r;
     



    return 0;
}





// int a=500,*p;
    // cout<<"value-:"<<a;
    // cout<<endl<<"Address:-"<<&a;
    // p=&a;
    // cout<<endl<<"value-:"<<*p;
    // cout<<endl<<"Address:-"<<p;
    // ++*p;
    // a=a-10;
    // cout<<endl<<*p;

Program 2

// Pointers in CPP
#include<iostream>
using namespace std;
int main()
{
    system("cls");
     int *i;
    char *c;
    float *f;
    double *d;
    cout<<sizeof(i)<<endl;  //4
    cout<<sizeof(c)<<endl;  //4
    cout<<sizeof(f)<<endl;  //4
    cout<<sizeof(d)<<endl;  //4
cout<<"---------------------------"<<endl;
    cout<<sizeof(*i)<<endl;   //4
    cout<<sizeof(*c)<<endl; //1
    cout<<sizeof(*f)<<endl;  //4
    cout<<sizeof(*d)<<endl;  //4



    return 0;
}

Program 3

// Pointers in CPP
#include<iostream>
using namespace std;
using namespace std;
int main()
{
    system("cls");
    int a=10,*p,**r;
    p=&a;
    r=&p;
    cout<<endl<<"a="<<a;   //10 
    cout<<endl<<"p="<<p;  //address of a
    cout<<endl<<"*p="<<*p;  //10
    cout<<endl<<"r="<<r;   //address of p
    cout<<endl<<"*r="<<*r; //address of a
    cout<<endl<<"**r="<<**r; // value of a
     ++**r;
     cout<<endl<<"a="<<a;   //10 
return 0;

}
Exit mobile version