Print size of different types Using Pointer in C++

Definition:

  • Pointer is a variable that holds the address of another variable.
  • Pointer variable also takes same memory as of actual data type.

Pointer Syntax: 

pointer_vaibale = &variable; 

Sizeof Definition:

sizeof returns the size of a variable or data type

Sizeof Syntax

sizeof (data_type)

Simple Program for Print size of different types Using Pointer in C++

/* Simple Program for Print size of different types Using Pointer in C++*/
/* Print Pointer Address C++ Program,C++ Pointer Examples */

// Header Files
#include <iostream>
#include<conio.h>

using namespace std;

int main() {

   // Declare Variables
   int a = 10;
   int *pa = &a;

   char b = 'x';
   char *pb = &b;

   float c = 10.01;
   float *pc = &c;

   double d = 10.01;
   double *pd = &d;

   long e = 10.01;
   long *pe = &e;

   cout << "Pointer C++ Example Program : Print Size of Different types Using sizeof\n";

   cout << "\n[sizeof(a)   ]: = " << sizeof (a);
   cout << "\n[sizeof(*pa) ]: = " << sizeof (*pa);

   cout << "\n[sizeof(b)   ]: = " << sizeof (b);
   cout << "\n[sizeof(*pb) ]: = " << sizeof (*pb);

   cout << "\n[sizeof(c)   ]: = " << sizeof (c);
   cout << "\n[sizeof(*pc) ]: = " << sizeof (*pc);

   cout << "\n[sizeof(d)   ]: = " << sizeof (d);
   cout << "\n[sizeof(*pd) ]: = " << sizeof (*pd);

   cout << "\n[sizeof(e)   ]: = " << sizeof (e);
   cout << "\n[sizeof(*pe) ]: = " << sizeof (*pe);

   return 0;
}

Sample Output:

Pointer C++ Example Program : Print Size of Different types Using sizeof

[sizeof(a)   ]: = 4
[sizeof(*pa) ]: = 4
[sizeof(b)   ]: = 1
[sizeof(*pb) ]: = 1
[sizeof(c)   ]: = 4
[sizeof(*pc) ]: = 4
[sizeof(d)   ]: = 8
[sizeof(*pd) ]: = 8
[sizeof(e)   ]: = 4
[sizeof(*pe) ]: = 4