Example program for pointer in C C++

Coding in C

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

Coding in C++

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

Output

Pointer program output in C CPP

Published by

admin

I am a computer teacher, programmer and web developer