我写了以下代码:
#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;
}这个程序的目的是打印出基本数据类型的大小,我想我已经完成了。此程序的另一个目的是将指针的大小打印到这些数据类型中的每一个。我很难搞清楚该怎么做。我知道指针是一个变量,它存储另一个变量的地址,并且指针涉及到尊重运算符(*)。有没有人能提个建议?我不是在寻找答案,只是在正确的方向上推动。
发布于 2014-06-22 07:45:32
int *p; // p is a pointer to an int因此指针的大小为:sizeof p,您可以将其打印为:
cout << "The size of int pointer is: " << sizeof p << endl;这就是你需要做的,打印其他指针的大小。
仅当您想要访问指针所指向的内容时,才会执行取消引用。例如。
int i = 5;
int *p = &i;
*p = 6;
*p = *p + 1;
//etc在这里,您只需要获取指针的大小。因此不需要取消引用。
https://stackoverflow.com/questions/24346809
复制相似问题