注意:
cout << sizeof(&str)<<endl; //在 VC++ 是 40 ,在Linux 上G++ GCC 是计算机的所对应指针大小
#include<iostream>
#include<stdio.h>
using namespace std;
void fun (int str[10])
{
cout << sizeof(str)<<endl; //4 数组名当为形参传递时,相当指向
//数组首元素首地址的指针,这是避免拷贝
//浪费内存
}
int main (int argc,char *argv[])
{
char element ;
element = 'a';
cout <<sizeof(&element)<<endl;
int *p = NULL;
int a = 9;
p = &a;
/* 强制类型转换()与sizeof() 优先级是同一级别 */
//cout <<sizeof((int )* *p);//错误
cout <<sizeof(int ) * *p<<endl; //4*9 == 36
cout <<sizeof((int *)p)<<endl; //指针的大小
char *q = NULL;
char des = 'a';
q = &des;
cout<< sizeof(q)<<endl; //指针的大小
cout <<sizeof(*q)<<endl; //指类的大小
int str[10];
cout <<sizeof(str)<<endl; //整个数组的大小,数组名为数组的整个空间大小
cout <<sizeof(str[9])<<endl; //第10个元素的大小,
cout << sizeof(&str)<<endl; //在 VC++ 是 40 ,在Linux 上G++ GCC 是计算机的所对应指针大小
cout << sizeof(&str[0])<<endl; //4
fun(str);
return 0;
}
运行结果:
4
36
4
4
1
40
4
40
4
4
Press any key to continue