c 语言 求数组大小

来源:百度知道 编辑:UC知道 时间:2024/06/14 19:35:19
#include <stdio.h>
void size(double[]);
void main()
{double b1[2]={1.5,2};
size(b1);}
void size(double a1[])
{int s;
s=sizeof a1/sizeof(double);
printf("%d\n",s);}
得到的结果总是零,貌似是由于sizeof只能求出指针的大小,如何才能编写函数求double 数组的大小呢?(必须自定义一个函数求主函数部分数组的大小)
必须定义为函数,这是最基本的要求。。。

void size(double a1[])
这里a1退化为指针,sizeof(a1)=4;
s=sizeof a1/sizeof(double)=4/8,s为int型.所以结果一直都是s=0.
------------------------------------------------------------------
void size(double a1[],int n)
{int s=n*sizeof(double);
-printf("%d\n",s);
} \\如果真要函数调用,则需两个形参.

#include <stdio.h>

void size(double a1[])
{
int s;
s=(sizeof (a1))*sizeof(double);
printf("%d\n",s);
}

void main()
{
double b1[2]={1.5,2};
size(b1);
}

我运行的结果
32
Press any key to continue

由于涉及到数组的降价问题(关于数组降价,可参照http://hi.baidu.com/only_hoo/blog/item/07d88695b04c8e1bd31b7018.html),因而在C中用sizeof()的方法不能解决此问题。

你要求该数组占多少字节吗?若是,我觉得应这样做
#include <stdio.h>
int size(double a[],int i);
int main()
{