
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Size of a Pointer in C/C++
The size of a pointer in C/C++ is not fixed. It depends upon different issues like Operating system, CPU architecture etc. Usually it depends upon the word size of underlying processor, for example for a 32 bit computer the pointer size can be 4 bytes and for a 64 bit computer the pointer size can be 8 bytes. So for a specific architecture pointer size will be fixed.
It is common to all data types like int *, float * etc.
Depending on the system architecture, pointer size may vary. The following table shows the pointer size based on the different system architecture:
Architecture | Size of Pointer |
---|---|
16-bit system | 2 bytes |
32-bit system | 4 bytes |
64-bit system | 8 bytes |
Finding Size of a Pointer
To find the size of a pointer, you can use the sizeof() operator by passing the pointer name. The sizeof() operator returns the size (number of bytes occupied by the memory) of any variable provided in it.
Example
In this example, we have declared and initialized the different type of pointers and demonstrating their sizes:
#include<stdio.h> struct str {}; void f(int a, int b) {} int main() { int a = 10; char c = 'G'; struct str s; // Pointers to different types int* ptr_int = &a; char* ptr_char = &c; struct str* ptr_str = &s; void (*ptr_func)(int, int) = &f; void* ptr_void = NULL; // Printing sizes of different pointers printf("Size of Integer Pointer: %lu bytes\n", sizeof(ptr_int)); printf("Size of Character Pointer: %lu bytes\n", sizeof(ptr_char)); printf("Size of Structure Pointer: %lu bytes\n", sizeof(ptr_str)); printf("Size of Function Pointer: %lu bytes\n", sizeof(ptr_func)); printf("Size of Void Pointer: %lu bytes\n", sizeof(ptr_void)); return 0; }
Following is the output to the above program:
Size of Integer Pointer: 8 bytes Size of Character Pointer: 8 bytes Size of Structure Pointer: 8 bytes Size of Function Pointer: 8 bytes Size of Void Pointer: 8 bytes
#include<iostream> using namespace std; struct str {}; void f(int a, int b) {} int main() { int a = 10; char c = 'G'; str s; // Pointers to different types int* ptr_int = &a; char* ptr_char = &c; str* ptr_str = &s; void (*ptr_func)(int, int) = &f; void* ptr_void = nullptr; // Printing sizes using sizeof cout<<"Size of Integer Pointer: "<<sizeof(ptr_int)<<" bytes"<<endl; cout<<"Size of Character Pointer: "<<sizeof(ptr_char)<<" bytes"<<endl; cout<<"Size of Structure Pointer: "<<sizeof(ptr_str)<<" bytes"<<endl; cout<<"Size of Function Pointer: "<<sizeof(ptr_func)<<" bytes"<<endl; cout<<"Size of Void Pointer: "<<sizeof(ptr_void)<<" bytes"<<endl; return 0; }
Following is the output to the above program:
Size of Integer Pointer: 8 bytes Size of Character Pointer: 8 bytes Size of Structure Pointer: 8 bytes Size of Function Pointer: 8 bytes Size of Void Pointer: 8 bytes