C++ supports various types of data types, which is categorized into built-in type, derived-type, and user-defined type. Size specifies what type of data or how much memory of that type of value requires.From this, it is clear that for representing various types of data on the computer it is, indeed, necessary to have different datatypes. We know that every datatype has specific size, that is, char has size of 1 byte, integer has 2 byte float has 4 byte and so on. But, the size depends on machine architecture(32-bit, 64-bit)too. Yes, if you declare an integer variable the size would be 2-byte(on a 32-bit machine) and 4-byte(on a 64-bit machine). Same is the case with pointer variable, any type you declare as a pointer it got the memory space depend on the machine. And all the pointer would be of the same size.
#include<iostream>
using namespace std;
void main(){
char ch;
int i;
float f;
double d;
char *chp;
int *ip;
float *fp;
double *dp;
long int *lip;
cout<<"Size of character pointer ="<<sizeof(ch)<<"\n";
cout<<"Size of integer ="<<sizeof(i)<<"\n";
cout<<"Size of float ="<<sizeof(f)<<"\n";
cout<<"Size of double ="<<sizeof(d)<<"\n";
cout<<"\tPOINTERS\n";
cout<<"Size of char "<<sizeof(chp)<<"\n";
cout<<"Size of integer "<<sizeof(ip)<<"\n";
cout<<"Size of float "<<sizeof(fp)<<"\n";
cout<<"Size of double "<<sizeof(dp)<<"\n";
cout<<"Size of long integer "<<sizeof(lip);
cout<<"\n";
return;
}
Ouput:
As you can see the size of data type and the pointer variable. For pointer variable, all have the same size.
In fact, a pointer which holds the memory address/location id is assigned by machine, and have the size which is machine dependent. Note that, it is a pointer, not a data type. As by definition- pointer is a variable which holds the memory address of another variable. So, the pointer of type char, float, double and integer et cetera would have the equal size.
No comments:
Post a Comment