您的位置:程序门 -> c/c++ -> c++ 语言



数组结构按地址传递怎么实现


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


数组结构按地址传递怎么实现[无满意答案结贴,结贴人:mzf333]
发表于:2007-08-31 17:19:15 楼主
//以下是我的程序,怎样修改caculate函数,使他按地址传递?
#include   <iostream>
using   namespace   std;
const   int   size=20;
struct   box
{
char   maker[40];
float   height;
float   width;
float   length;
float   volume;

};
void   output(box   x[],int   m);
double   caculate(box   p[],int   lim);

int   main()
{
        int   n;
cout < < "how   many   records   do   you   want   to   enter:\n ";
cin> > n;
box   market[size];
for(int   i=0;i <n;i++)
{
cout < < "maker: ";
cin> > market[i].maker;
cout < < "height: ";
cin> > market[i].height;
cout < < "width: ";
cin> > market[i].width;
cout < < "length: ";
cin> > market[i].length;
cout < <endl;
}

market[i].volume=caculate(market,n);
        output(market,n);  


cout < < "done!\n ";
return   0;
}

void   output(box   x[],int   m)
{
        for(int   i=0;i <m;i++)
{
cout < < "maker: " < <x[i].maker < <endl;
        cout < < "height: " < <x[i].height < <endl;
        cout < < "width: " < <x[i].width < <endl;
        cout < < "length: " < <x[i].length < <endl;
        cout < < "volume: " < <x[i].volume < <endl;
cout < <endl;
}
}

double   caculate(box   p[],int   lim)
{       for(int   i=0;i <lim;i++)    
p[i].volume=p[i].length*p[i].width*p[i].height;
return   p[i].volume;
}
发表于:2007-08-31 17:23:591楼 得分:0
double   caculate(box   p[],int   lim);
你这样声明已经就是传数组地址了呀
发表于:2007-08-31 17:33:242楼 得分:0
数组名就是一个常量指针!
发表于:2007-08-31 17:51:393楼 得分:0
对不起,是用new来做怎么做?
发表于:2007-08-31 17:55:544楼 得分:0
一样的,直接把指针传给caculate函数
a   =   new   box[n];
caculate(a,   n);
delete   []   a;


快速检索

最新资讯
热门点击