基本数据类型程序

Fundamental Data Types Program

本文关键字:程序 数据类型      更新时间:2023-10-16

我写了以下代码:

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
    char c;
    int i;
    short int j;
    long int k;
    float f;
    double d;
    long double e;
    cout << "The size of char is: " << sizeof c << endl;
    cout << "The size of int is: " << sizeof i << endl;
    cout << "The size of short int is: " << sizeof j << endl;
    cout << "The size of long int is: " << sizeof k << endl;
    cout << "The size of float is: " << sizeof f << endl;
    cout << "The size of double is: " << sizeof d << endl;
    cout << "The size of long double is: " << sizeof e << endl;
    system("pause");
    return 0;
}

这个程序的目的是打印出基本数据类型的大小,我认为我已经完成了。这个程序的另一个目的是输出指向这些数据类型的指针的大小。我不知道该怎么做。我知道指针是存储另一个变量的地址的变量,指针涉及到差分操作符(*)。谁能提个建议?我不是在寻找答案,只是在正确的方向上轻轻一推。

int *p; // p is a pointer to an int

所以指针的sizeof是:sizeof p,可以打印为:

cout << "The size of int pointer is: " << sizeof p << endl;

这是你需要做的打印其他指针的大小

解引用仅在想要访问指针所指向的对象时才执行。例如

int i = 5;
int *p = &i;
*p = 6;
*p = *p + 1; 
//etc

这里,你只想得到指针的大小。因此不需要解引用