Write a program to perform Pointer Arithmetic operations

/*
    Pointer Arithmetic
*/
#include<iostream>
#include<process.h>
#include<stdio.h>
using namespace std;

int main()
{
    int a=10,*ptr,ch;

    while(1)
    {
        cout<<"\n\n Menu:";
        cout<<"\n 1. Addition ";
        cout<<"\n 2. Subtraction ";
        cout<<"\n 3. Increment ";
        cout<<"\n 4. Decrement ";
        cout<<"\n 5. Exit";
        cout<<"\n\n Enter your choice(1/2/3/4): ";
        cin>>ch;

        ptr=&a;

        cout<<"\n\n Before Operation";
        cout<<"\n Value:  "<<*ptr;
        cout<<"\n Address: "<<ptr;
        cout<<"\n Address of pointer: "<<&ptr;

        switch(ch)
        {
            case 1: ptr=ptr+2;
                    break;

            case 2: ptr=ptr-2;
                    break;


            case 3: ptr++;
                    break;

            case 4: ptr--;
                    break;

            case 5: return(0);

            default: cout<<"\n\n Wrong Choice";
        }

        cout<<"\n\n After Operation";
        cout<<"\n Value:  "<<*ptr;
        cout<<"\n Address: "<<ptr;
        cout<<"\n Address of pointer: "<<&ptr;

    }
    return 0;
}

Previous
Next Post »