在程序中先声明int,float,char,double型的变量,再声明 int,float,char,double类型的指针变量,并用相应存储空间初始化指针变量,分别显示这些指针变量占了多少字节,指针变量所指的变量占了多少字节。用sizeof运算符
时间: 2024-02-03 08:15:53 浏览: 72
Keil MDK-ARM各种数据类型占用的字节数 char short int float double
5星 · 资源好评率100%
以下是程序示例:
```c++
#include <iostream>
using namespace std;
int main() {
int i = 10;
float f = 3.14;
char c = 'a';
double d = 3.1415926;
int *ip = &i;
float *fp = &f;
char *cp = &c;
double *dp = &d;
cout << "Size of int pointer: " << sizeof(ip) << " bytes" << endl;
cout << "Size of float pointer: " << sizeof(fp) << " bytes" << endl;
cout << "Size of char pointer: " << sizeof(cp) << " bytes" << endl;
cout << "Size of double pointer: " << sizeof(dp) << " bytes" << endl;
cout << "Size of int variable: " << sizeof(i) << " bytes" << endl;
cout << "Size of float variable: " << sizeof(f) << " bytes" << endl;
cout << "Size of char variable: " << sizeof(c) << " bytes" << endl;
cout << "Size of double variable: " << sizeof(d) << " bytes" << endl;
return 0;
}
```
输出:
```
Size of int pointer: 8 bytes
Size of float pointer: 8 bytes
Size of char pointer: 8 bytes
Size of double pointer: 8 bytes
Size of int variable: 4 bytes
Size of float variable: 4 bytes
Size of char variable: 1 bytes
Size of double variable: 8 bytes
```
可以看到,指针变量占用8字节,而变量i和f占用4字节,变量c占用1字节,变量d占用8字节。
阅读全文