热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

数组作为函数参数进行传递

今天编程序时发生了个这样的错误:在头文件里定义了一个数组:ViewCode1chars[]1234567890;又定义了一个现显示组的函数ÿ

今天编程序时发生了个这样的错误:

在头文件里 定义了一个数组:

View Code

1 char s[]="1234567890";

又定义了一个现显示组的函数:

View Code

1 void Display(char* c);

通过下面这两条语句分别在现实函数和主函数中现实数组的大小:

View Code

1 sizeof(c);
2 sizeof(s);

现实结果却大相径庭,在主函数中为11,在现实函数中却为4。

经过思考发现,在主函数中s代表的是一个数组,而在现实函数中c代表的是一个指向数组头的指针。数组的大小为11,指针的大小为4。

主程序如下:

View Code

1 #include
2 #include
3 using namespace std;
4
5 char s[]="1234567890";
6 void Display(char* c);
7
8 void main()
9 {
10 char a;
11 cout<<"这个是在主函数中对数组长度的测试&#xff1a;"<<sizeof(s)<12 Display(s);
13 cin>>a;
14 }
15
16
17 void Display(char* c)
18 {
19 cout<<"这个是在子函数中对数组长度的测试&#xff1a;"<<sizeof(c)<20 }

现实结果&#xff1a;

我的问题是怎样可以在主函数和子函数中都指的是数组而不是一个指向数组头的指针&#xff1f;&#xff1f;&#xff1f;

 

 

在网上查了几种将数组作为函数参数进行传递的方法&#xff0c;列举如下&#xff1a;

View Code

1 #include
2 #include
3 #include
4 using namespace std;
5
6 char s[]&#61;"1234567890";
7 int a[10]&#61;{0,1};
8 int b[10]&#61;{0,1};
9 void Display(char* c);
10 void PutArray1(int *p,int length1);
11 void PutArray2(int p[],int length1);
12 void PutArray3(int p[10]);
13 void PutArray4(int (&p)[10]);
14 void PutArray5(vector<int>verc);
15
16 void main()
17 {
18 char q;
19 cout<<"这个是在主函数中对数组长度的测试&#xff1a;"<<sizeof(s)<20 Display(s);
21 cout<<"*********************************************"<22 PutArray1(a,10);
23 PutArray2(a,10);
24 PutArray3(a);
25 PutArray4(b);
26 cin>>q;
27 }
28
29
30 void Display(char* c)
31 {
32 cout<<"这个是在子函数中对数组长度的测试&#xff1a;"<<sizeof(c)<33 }
34 void PutArray1(int *p,int length1)
35 {
36 int length2&#61;sizeof(p);
37 cout<<"第一种方法的输出&#xff1a;"<38 cout<<"第一种方法数组的长度为&#xff1a;"<39 for(int i&#61;0;i40 {
41 cout<42 }
43 cout<44 }
45 void PutArray2(int p[],int length1)
46 {
47 int length2&#61;sizeof(p);
48 cout<<"第二种方法的输出&#xff1a;"<49 cout<<"第二种方法数组的长度为&#xff1a;"<50 for(int i&#61;0;i51 {
52 cout<53 }
54 cout<55 }
56 void PutArray3(int p[10])
57 {
58 int length2&#61;sizeof(p);
59 cout<<"第三种方法的输出&#xff1a;"<60 cout<<"第三种方法数组的长度为&#xff1a;"<61 for(int i&#61;0;i<9;i&#43;&#43;)
62 {
63 cout<64 }
65 cout<66 }
67 void PutArray4(int (&p)[10])
68 {
69 int length2&#61;sizeof(p);
70 cout<<"第四种方法的输出&#xff1a;"<71 cout<<"第四种方法数组的长度为&#xff1a;"<72 for(int i&#61;0;i<9;i&#43;&#43;)
73 {
74 cout<75 }
76 cout<77 }
78 void PutArray5(vector<int>verc)
79 {
80 vector<int>::iterator begin_iter&#61;verc.begin();
81 vector<int>::iterator end_iter&#61;verc.end();
82 int size&#61;verc.size();
83 cout<<"第五种方法的输出&#xff1a;"<84 cout<<"第五种方法数组的长度为&#xff1a;"<85 cout<<"下面这种方法是采用向量遍历的方法遍历数组&#xff1a;"<86 for(vector<int>::iterator iter&#61;begin_iter;iter!&#61;end_iter;iter&#43;&#43;)
87 {
88 cout<<*iter;
89 }
90 cout<91 cout<<"下面这种方法是采用普通遍历数组的方法遍历数组&#xff1a;"<92 for(int i&#61;0;i1;i&#43;&#43;)
93 {
94 cout<95 }
96 cout<97 }

在这里&#xff0c;int *arr和int arr[]的含义相同&#xff0c;编译器自动将 int arr[]替换为int *arr&#xff0c;所以这也解释了上面在主函数和子函数中利用数组名求数组长度会得到不同结果的原因。这种情况只有在数组作为函数参数进行传递时才会发生&#xff08;C&#43;&#43; Primer Plus,P192&#xff09;。

其中第四种方法没有理解&#xff0c;疑问暂时留在这里吧。

另外虽然上面四种方法都可以正确地在子函数中传递数组作为参数&#xff0c;但是仍然不能满足博客刚开始的要求&#xff1a;在子函数中可以测的参数数组的长度。后来查看C&#43;&#43; Primer Plus发现书上已经明确指出没有实现这种想法的方法&#xff0c;数组的长度必须在函数中作为参数进行传递。


另外由于第五种方法需要重新定义向量模版&#xff0c;和主题不符&#xff0c;所以在主函数里并没有调用它。例程中给的程序如下所示&#xff1a;

View Code

1 vector<int> verc1(a,a&#43;10);
2 vector<int> verc2(b,b&#43;8);
3 PutArray5(verc1);
4 PutArray5(verc2);

 上面这五种调用数组的方法只是在传递数组的方式上不同&#xff0c;可以归纳为传递数组的一种方法&#xff0c;即&#xff1a;传递指向数组头的指针和数组的长度。另外一种传递数组的方法是将指向数组头的指针和指向数组尾的指针作为两个参数进行传递&#xff0c;函数定义如下&#xff1a;

View Code

1 int sum_arr(const int* begin,const int* end);



在学习这个知识点时是有一些感想的&#xff0c;虽说C&#43;&#43; Primer已经看了一遍&#xff0c;里面的知识点也认真地消化过&#xff0c;但是一到用的时候就使不上劲。看来应该在看书的同时多实践书上的代码&#xff0c;这样才能对代码的应用有更好的理解。

 






转载于:https://www.cnblogs.com/tziyachi/archive/2012/02/11/2347073.html


推荐阅读
author-avatar
2674224139_8a6503
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有