首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于数组大小的有关问题

2012-12-30 
关于数组大小的问题void Fun(char a[100]){coutsizeof(a)endl}char str[100]coutsizeof(str)end

关于数组大小的问题


void Fun(char a[100])
{
     cout<<sizeof(a)<<endl;
}



    char str[100];
    cout<<sizeof(str)<<endl;

这两段代码为什么一个是4一个是100?菜鸟求教,勿喷。。。
[解决办法]
所以sizeof指针,指针占4个字节。
[解决办法]
void Fun(char a[100]);这种数组做形参,数组被当成指针传入函数,所以写不写数组的大小是没关系的,它与下面两人种等价
void Fun(char a[]);     不写大小
void Fun(char *a);  上面两种最终都被解释成这种指针形式。。

[解决办法]
void Fun(char a[100]) {      cout<<sizeof(a)<<endl; } 
在这里,数组做函数参数时,会退化为第一个元素的指针,指针的大小就是4了。
[解决办法]
数组左值作为sizeof的参数不退化。其它大部分情况下,包括作为参数时,转换为指向首个元素的指针值。
[解决办法]
引用
When the sizeof operator is applied to an array, it yields the total number of bytes in 
that array, not the size of the pointer represented by the array identifier. To obtain the size 
of the pointer represented by the array identifier, pass it as a parameter to a function that 
uses sizeof.
.

热点排行