C++ Program to Print Pointer Size of Data Types

This C++ program prints the sizes of pointers to primitive datatypes like int, float, double and long. The size of the pointers is implementation dependent and should be same for every primitive datatype since it contains the same of data. The sizeof operator is used for determining the size of the datatypes.

Here is the source code of the C++ program which prints the sizes of the pointers to primitive datatypes like int, float, double and long. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Print Sizes of Pointers to Primitive Datatypes
  3.  */
  4. #include <iostream>
  5.  
  6. int main()
  7. {
  8.     int * ip;
  9.     long * lp;
  10.     char * cp;
  11.     float * fp;
  12.     double * dp;
  13.  
  14.     std::cout << "Datatype  " << "Pointer Size\n" << std::endl
  15. 	      << " int" << std::setw(12) << sizeof(ip) << std::endl
  16.               << " long" << std::setw(11)<< sizeof(lp) << std::endl
  17. 	      << " char" << std::setw(11) << sizeof(cp) << std::endl
  18. 	      << " float" << std::setw(10) << sizeof(fp) << std::endl
  19.               << " double" << std::setw(9) << sizeof(dp) << std::endl;
  20.    return 0;	
  21. }

$ a.out
Datatype  Pointer Size
 
 int           4
 char          4
 long          4
 float         4
 double        4

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.