Example program for pointer to pointer in C C++

Coding in C

#include<stdio.h>
int main()
{
    int num=5;
    int *ptr1;
    ptr1=#
    int **ptr2;
    ptr2=&ptr1;
    printf("Value of num   = %d",num);
    printf("\nAddress of num = %p",&num);
    printf("\nValue of num   = %d",*ptr1);
    printf("\nAddress of num = %p",ptr1);
    printf("\nValue of num   = %d",**ptr2);
    printf("\nAddress of num = %p",*ptr2);
    return 0;
}

Coding in C++

#include<iostream>
using namespace std;
int main()
{
    int num=5;
    int *ptr1;
    ptr1=#
    int **ptr2;
    ptr2=&ptr1;
    cout<<"Value of num   = "<<num<<endl;
    cout<<"Address of num = "<<&num<<endl;
    cout<<"Value of num   = "<<*ptr1<<endl;
    cout<<"Address of num = "<<ptr1<<endl;
    cout<<"Value of num   = "<<**ptr2<<endl;
    cout<<"Address of num = "<<*ptr2<<endl;
    return 0;
}

Output

Pointer to pointer program output in C CPP

Published by

admin

I am a computer teacher, programmer and web developer