c语言函数调用过程中如何在子函数中调用主体函数的结构数组

老衲只是想在子函数中调用主函数的数组而已为何是地址【c语言吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:579,417贴子:
老衲只是想在子函数中调用主函数的数组而已为何是地址收藏
关于这个子函数调用的问题我很纠结特别是调用数组的时候我就的定义指针因为地址传递才可以把原来的数据修改,但是有木有不用指针就可以把原来的值修改了呢我的意思就是把主函数的数组直接传递到子函数中不对其操作然后直接再返回修改后的数组给主函数。这个行得通不?
2楼附图。并说明我用意我试图直接传递数组a[4][4]到子函数中去,然后在子函数中对其操作并返回这个数组给主函数,但是结果如图我穿进去的是个地址是a[0][0]的的地址,所以说操作都白搭啦要是不用指针的话我怎么才能在子函数中对那个数组进行加工呢?
你们都还没睡醒吗。赶紧来给我解惑啊
楼主您好,函数的参数可以用数组的,但是本质上和指针没有区别。比如你写int mm(int a[]),意思就是int mm(int *a),因为数组名字可以看做是“不变的指针”。之后在函数体内对数组的操作也就是对实际的那个数组进行操作,不需要返回值。另外函数不能返回一个数组,只能返回指针。
找师太问吧。
数组名相当于指针。
这是关键的
难道你们写子函数的时候都没有遇到这个问题吗怎么不来个高手解决问题那来来来
登录百度帐号c语言如何在子函数中改变主函数数组值?_百度知道
c语言如何在子函数中改变主函数数组值?
double Y[8];
void rk();
/* 调用rk函数*/
/* h为步长*/
fi=fopen(&C:\\Users\\Administrator\\Desktop\\input2.dat&,&r&);
for(i=0;i&8;i++)
fscanf(fi,&%lf&,&Y[i]); /* 从文件中读取Y数组值*...
我有更好的答案
两种方法.一:把a作为参数传递给sss();int sss(int x){}这样仅仅只是使用a的值,是把a的值复制给x,使x的值等于a,可以在子函数中使用a的数值,但无法改变主函数中a的值.二,使用指针.声明一个指针指向a,把a的地址作为参数传递给子函数,那么在子函数中不但可以使用a的值还可以修改主函数中a的值.
采纳率:73%
来自团队:
你的程序恐怕连编译都不能通过吧,有语法错误。
我源程序能编译 这不是简化了 随便打得 主要是看数组值怎么变啊
你最好把你的可以编译的程序贴上
原程序几百行你看吗,再说我上面这个程序就少了个#include和FILE *fi 就能编译
你重新看下rk函数,是不是里面又定义了一个名为Y的局部变量。你可以rk函数里试试直接更改Y[0]的值看是否会改变还有,你的h值是否为0
本回答被提问者采纳
变量有全局变量和局部变量之分,要想子函数改变母函数的变量值有两种办法:1是将变量定义成全局变量(在#include下面就定义),然后在子函数中直接使用变量名。2是将指向该变量的指针当作参数传递给子函数。
我这个Y[8]是在#include下面定义的,为什么不能改变他的值
函数定义语句void rk(n,h)中n和h没有类型
有啊 大哥 再说这个跟数组赋值有什么关系
你那个h好像始终是0吧
其他1条回答
为您推荐:
其他类似问题
主函数的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。在C语言中怎么将子函数中的数组和函数调用出来?_百度知道
在C语言中怎么将子函数中的数组和函数调用出来?
在C语言中怎么将子函数中的数组和函数调用出来?
void shuru(){
int a=123;
int a[10]={1,2,3,4,5,4,6,7};
return a[10]
void main(){
printf(&%d&,b);
for(int i=0;i&10;i++){
printf(&%d&,a[i]);
这是大体意思,是错的...
我有更好的答案
首先这样的作法是没有意义的,因为当函数返回之后,函数原始空间中的数组和变量是临时的,都会被自动清除和释放。如果需要通过函数将改变的结果传回,可以使用指针。void shuru(int* p,int a[]){*p = 123;int b[10]={1,2,3,4,5,4,6,7};for (int i=0;i&9;i++) a[i]=b[i]}void main(){int b, a[10] ;shuru(&b,a);printf(&%d&,b);for(int i=0;i&10;i++){printf(&%d&,a[i]);}}
为您推荐:
其他类似问题
函数调用的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。C语言 主函数中输入数组 怎样在子函数中调用该数组值
[问题点数:20分]
C语言 主函数中输入数组 怎样在子函数中调用该数组值
[问题点数:20分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
匿名用户不能发表回复!|欢迎加入我们,一同切磋技术 &
用户名: &&&
密 码: &
共有 8172 人关注过本帖
标题:关于结构体数组作函数参数的问题
等 级:新手上路
&&已结贴√
&&问题点数:20&&回复次数:8&&&
关于结构体数组作函数参数的问题
有一段程序:
程序代码:#include &stdio.h&
struct Students
&&&&&&&&int
&&&&&&&&int
void print_under_sixty(struct Stduent *stds,int t)
&&& int i,
&&& printf(&================\n&);
&&& printf(&name&&& score\n&);
&&& for(i=<font color=#;i&=t;i++)
&&&&&&&&if(stds.score[i] & <font color=#)
&&&&&&&&&&&&count++;
&&&&&&&&&&&&printf(&%d%10d\n&,stds[i].num,stds[i].score);
&&& printf(&the count of under sixty : %d\n&,count);
void main()
&&& int total = <font color=#,i;
&&&&&&&&Students std[<font color=#];
&&& for(i=<font color=#;i&<font color=#;i++)
&&&&&&&&printf(&student %d &,i);
&&&&&&&&std[i].num =
&&&&&&&&scanf(&%d&,&std[i].score);
&&&&&&&&if(std[i].score & <font color=#)
&&&&&&&&&&&&break;
&&&&&&&&total++;
&&& printf(&num&&& score\n&);
&&& for(i=<font color=#;i&=i++)
&&&&&&&&printf(&%d%10d\n&,std[i].num,std[i].score);
&&& print_under_sixty(std,total);
print_under_sixty里面好多错误,求大神分析
搜索更多相关主题的帖子:
等 级:贵宾
威 望:304
帖 子:25793
专家分:48814
if(stds.score[i] & 60)
授人以渔,不授人以鱼。
等 级:新手上路
刚学这个东西,也不知道对不对!
void print_under_sixty(struct Students *stds,int t)
&&& int i,count=0;
&&& printf(&================\n&);
&&& printf(&name&&& score\n&);
&&& for(i=1;i&=t;i++)
&&&&&&&&if(stds[i].score & 60)
&&&&&&&&&&&&count++;
&&&&&&&&&&&&printf(&%d%10d\n&,stds[i].num,stds[i].score);
&&& printf(&the count of under sixty : %d\n&,count);
void main()
&&& int total = 0,i;
&&&&&&struct Students std[30];
&&& for(i=0;i&30;i++)
&&&&&&&&printf(&student %d &,i);
&&&&&&&&std[i].num = i+1;
&&&&&&&&scanf(&%d&,&std[i].score);
&&&&&&&&if(std[i].score & 0)
&&&&&&&&&&&&
&&&&&&&&total++;
&&& printf(&num&&& score\n&);
&&& for(i=1;i&=i++)
&&&&&&&&printf(&%d%10d\n&,std[i-1].num,std[i-1].score);
&&& print_under_sixty(std,total);
等 级:新手上路
子函数中student写错了,看看下面的程序:
#include &stdio.h&
struct Students
void print_under_sixty(struct Students *std,int t)
&&& int count=0;
&&& printf(&================\n&);
&&& printf(&num&&&&&&score\n&);
&&& for (int i=0;i&t;i++)
&&&&&&&&if((std-&score)& 60)
&&&&&&&&&&&&count++;
&&&&&&&&&&&&printf(&%d%10d\n&,std-&num,std-&score);
&&&&&&&&std++;&&& //结构体数组指针指向下一个
&&& printf(&the count of under sixty is : %d\n&,count);
void main()
&&& int total = 0,i;
&&& struct Students std0[30];
&&& void print_under_sixty(struct Students *std,int t);
&&& for(i=0;i&30;i++)
&&&&&&&&printf(&student%d&&&,i);
&&&&&&&&std0[i].num =
&&&&&&&&scanf(&%d&,&std0[i].score);
&&&&&&&&if(std0[i].score== 0)
&&&&&&&&&&&&
&&&&&&&&total++;
&&& printf(&num&&&&&score\n&);
&&& print_under_sixty(std0,total);
等 级:蝙蝠侠
帖 子:356
专家分:954
程序代码:#include &stdio.h&
#define N 5&&&&&//这里可以换成 你想要的数值
struct Students
&&&&&&&&int
&&&&&&&&int
&&& }stu[N];
void print_under_sixty(struct Students *stu,int t)
&&& int i,count=<font color=#;
&&& printf(&================\n&);
&&& printf(&name&&& score\n&);
&&& for(i=<font color=#;i&=t;i++)
&&&&&&&&if(stu[i].score& <font color=#)
&&&&&&&&&&&&count++;
&&&&&&&&&&&&printf(&%d%10d\n&,stu[i].num,stu[i].score);
&&& printf(&the count of under sixty : %d\n&,count);
void main()
&&& int total = <font color=#;
&&& //struct Students&&stu[N];
&&& for(i=<font color=#;i&=N;i++)
&&&&&&&&printf(&student %d &,i);
&&&&&&&&stu[i].num=i;
&&&&&&&&scanf(&%d&,&stu[i].score);
&&&&&&&&if(stu[i].score & <font color=#)
&&&&&&&&&&&&break;
&&&&&&&&total++;
&&& printf(&num&&& score\n&);
&&& for(i=<font color=#;i&=i++)
&&&&&&&&printf(&%d%10d\n&,stu[i].num,stu[i].score);
&&& print_under_sixty(stu,total);
可以运行,你再看看还有什么不懂得地方吧
重要的不是结果,是求一个结果的过程,哪怕千难万难,当你有想要的结果时,你已走的很远
等 级:新手上路
帖 子:13
#include &stdio.h&
struct Students
void print_under_sixty(struct Students *stds,int t)
&&& int i,count=0;
&&& struct Students *p;
&&& printf(&================\n&);
&&& printf(&name&&& score\n&);
&&& for(p=stds+1;p&=stds+t;p++)
&&&&&&&&if(p-&score & 60)
&&&&&&&&&&&&count++;
&&&&&&&&&&&&printf(&%d%10d\n&,p-&num,p-&score);
&&& printf(&the count of under sixty : %d\n&,count);
void main()
&&& int total = 0,i;
&&& struct Students std[30];&&& //定义结构变量错误
&&& for(i=1;i&30;i++)
&&&&&&&&printf(&student %d &,i);
&&&&&&&&std[i].num =
&&&&&&&&scanf(&%d&,&std[i].score);
&&&&&&&&if(std[i].score & 0)
&&&&&&&&&&&&&&&&&&&&&&&&&&//这个将退出循环。这一步怎么想的?
&&&&&&&&total++;
&&& printf(&num&&& score\n&);
&&& for(i=1;i&=i++)
&&&&&&&&printf(&%d%10d\n&,std[i].num,std[i].score);
&&& print_under_sixty(std,total);
等 级:新手上路
帖 子:13
#include &stdio.h&
struct Students
void print_under_sixty(struct Students *stds,int t)
&&& int i,count=0;
&&& printf(&================\n&);
&&& printf(&name&&& score\n&);
&&& for(i=1;i&=t;i++)
&&&&&&&&if(stds[i-1].score & 60)
&&&&&&&&&&&&count++;
&&&&&&&&&&&&printf(&%d%10d\n&,stds[i-1].num,stds[i-1].score);
&&& printf(&the count of under sixty : %d\n&,count);
void main()
&&& int total = 0,i;
&&& struct Students std[30];
&&& for(i=0;i&30;i++)
&&&&&&&&printf(&student %d &,i);
&&&&&&&&std[i].num = i+1;
&&&&&&&&scanf(&%d&,&std[i].score);
&&&&&&&&if(std[i].score & 0)
&&&&&&&&&&&&
&&&&&&&&total++;
&&& printf(&num&&& score\n&);
&&& for(i=1;i&=i++)
&&&&&&&&printf(&%d%10d\n&,std[i-1].num,std[i-1].score);
&&& print_under_sixty(std,total);
等 级:新手上路
帖 子:13
struct Students
void print_under_sixty(struct Students *stds,int t)
&&& int i,count=0;
&&& printf(&================\n&);
&&& printf(&name&&& score\n&);
&&& for(i=1;i&=t;i++)
&&&&&&&&if(stds[i-1].score & 60)
&&&&&&&&&&&&count++;
&&&&&&&&&&&&printf(&%d%10d\n&,stds[i-1].num,stds[i-1].score);
&&& printf(&the count of under sixty : %d\n&,count);
void main()
&&& int total = 0,i;
&&& struct Students std[30];
&&& for(i=0;i&30;i++)
&&&&&&&&printf(&student %d &,i);
&&&&&&&&std[i].num = i+1;
&&&&&&&&scanf(&%d&,&std[i].score);
&&&&&&&&if(std[i].score & 0)
&&&&&&&&&&&&
&&&&&&&&total++;
&&& printf(&num&&& score\n&);
&&& for(i=1;i&=i++)
&&&&&&&&printf(&%d%10d\n&,std[i-1].num,std[i-1].score);
&&& print_under_sixty(std,total);
等 级:论坛游民
帖 子:35
专家分:66
5楼的答案不错哦
版权所有,并保留所有权利。
Powered by , Processed in 0.458054 second(s), 7 queries.
Copyright&, BCCN.NET, All Rights Reserved

我要回帖

更多关于 c语言函数调用数组 的文章

 

随机推荐