Print size of different types Using Pointer in C++
On this page (8sections)
About this program
This is an example program in c pointer example programs. Read the concept first: Pointer Example Program in C++, then study the code and output below.
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
Related Pages
Learn the concept first, then study the code:
- C++ Programs — Browse all C++ Programs.
- Pointer Example Program in C++ — Concept — pointers, addresses and dereferencing in C++.
- Simple Program for Print address of Variable Using Pointer in C++ — More in c pointer example programs.
- Pointer Simple Example Program with Reference operator (&) and Dereference operator (*) — More in c pointer example programs.