免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3892 | 回复: 6
打印 上一主题 下一主题

数组按值传递 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-12-17 17:44 |只看该作者 |倒序浏览
最近看书自学到数组这章..书上在打印数组的时候都是直接打印数组,然后我结合前面学的 想把数组的打印放到函数里面 “按值传递” 可是出现错误!


#include <iostream>

using namespace std;


void showArray(int souArray[5]){

int souArray[5] ; //提示语法错误
int temp;   
  for(temp=0;temp<=4;temp++){
  
   cout<<souArray[ temp ]<<"\n";
   
  }//END FOR

};//END SHOWARRAY


int main(){

int myArray[5] = {36,56,32,12,10};

showArray(int myArray[5]); //提示语法错误

/*
for (i=0;i<=4;i++){
  cout<<myArray[ i ]<<"\n";
}//END FOR
*/

}//END MAIN


编译的时候错误提示

g++ -o test array.cpp
array.cpp: In function `void showArray(int*)':
array.cpp:8: declaration of `souArray' shadows a parameter
array.cpp: In function `int main()':
array.cpp:23: syntax error before `[' token
array.cpp:31:12: warning: no newline at end of file

我按引入传入也不行 帮我整整 谢了 !

论坛徽章:
0
2 [报告]
发表于 2005-12-17 17:52 |只看该作者
那个定义去掉就行了

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
3 [报告]
发表于 2005-12-17 17:54 |只看该作者
你需要看看函数的参数是怎么传递的。
书上都没有一个传递数组做为参数的例子吗?

论坛徽章:
0
4 [报告]
发表于 2005-12-17 18:01 |只看该作者

回复 1楼 bbcode 的帖子

#include <iostream>
using namespace std;
void showArray(int souArray[5])
{
int temp;   
  for(temp=0;temp<=4;temp++)
  {
  cout<<souArray[ temp ]<<"\n";
   }
}


int main()
{

int myArray[5] = {36,56,32,12,10};

showArray(myArray);
return 0;
}

It's OK now.

论坛徽章:
0
5 [报告]
发表于 2005-12-17 19:43 |只看该作者
数组名代表数组的首地址,传递时不需要再加int.
你在showarray函数下无需要再定义数组.

论坛徽章:
0
6 [报告]
发表于 2005-12-17 19:45 |只看该作者
谢谢 clwwy
为什么在数组按值传递的时候 在函数里面不需要定义了?

论坛徽章:
0
7 [报告]
发表于 2005-12-17 19:46 |只看该作者
原帖由 誓不_低头 于 2005-12-17 19:43 发表
数组名代表数组的首地址,传递时不需要再加int.
你在showarray函数下无需要再定义数组.


   非常谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP