今天编程序时发生了个这样的错误:
在头文件里 定义了一个数组:
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 char s[]="1234567890";
又定义了一个现显示组的函数:
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 void Display(char* c);
通过下面这两条语句分别在现实函数和主函数中现实数组的大小:
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 sizeof(c); 2 sizeof(s);
现实结果却大相径庭,在主函数中为11,在现实函数中却为4。
经过思考发现,在主函数中s代表的是一个数组,而在现实函数中c代表的是一个指向数组头的指针。数组的大小为11,指针的大小为4。
主程序如下:
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 #include<iostream> 2 #include<stdlib.h> 3 using namespace std; 4 5 char s[]="1234567890"; 6 void Display(char* c); 7 8 void main() 9 { 10 char a; 11 cout<<"这个是在主函数中对数组长度的测试:"<<sizeof(s)<<endl; 12 Display(s); 13 cin>>a; 14 } 15 16 17 void Display(char* c) 18 { 19 cout<<"这个是在子函数中对数组长度的测试:"<<sizeof(c)<<endl; 20 }
现实结果:
我的问题是怎样可以在主函数和子函数中都指的是数组而不是一个指向数组头的指针???
在网上查了几种将数组作为函数参数进行传递的方法,列举如下:
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 #include<iostream> 2 #include<stdlib.h> 3 #include<vector> 4 using namespace std; 5 6 char s[]="1234567890"; 7 int a[10]={0,1}; 8 int b[10]={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<<"这个是在主函数中对数组长度的测试:"<<sizeof(s)<<endl; 20 Display(s); 21 cout<<"*********************************************"<<endl; 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<<"这个是在子函数中对数组长度的测试:"<<sizeof(c)<<endl; 33 } 34 void PutArray1(int *p,int length1) 35 { 36 int length2=sizeof(p); 37 cout<<"第一种方法的输出:"<<endl; 38 cout<<"第一种方法数组的长度为:"<<length2<<endl; 39 for(int i=0;i<length1;i++) 40 { 41 cout<<p[i]; 42 } 43 cout<<endl; 44 } 45 void PutArray2(int p[],int length1) 46 { 47 int length2=sizeof(p); 48 cout<<"第二种方法的输出:"<<endl; 49 cout<<"第二种方法数组的长度为:"<<length2<<endl; 50 for(int i=0;i<length1;i++) 51 { 52 cout<<p[i]; 53 } 54 cout<<endl; 55 } 56 void PutArray3(int p[10]) 57 { 58 int length2=sizeof(p); 59 cout<<"第三种方法的输出:"<<endl; 60 cout<<"第三种方法数组的长度为:"<<length2<<endl; 61 for(int i=0;i<9;i++) 62 { 63 cout<<p[i]; 64 } 65 cout<<endl; 66 } 67 void PutArray4(int (&p)[10]) 68 { 69 int length2=sizeof(p); 70 cout<<"第四种方法的输出:"<<endl; 71 cout<<"第四种方法数组的长度为:"<<length2<<endl; 72 for(int i=0;i<9;i++) 73 { 74 cout<<p[i]; 75 } 76 cout<<endl; 77 } 78 void PutArray5(vector<int>verc) 79 { 80 vector<int>::iterator begin_iter=verc.begin(); 81 vector<int>::iterator end_iter=verc.end(); 82 int size=verc.size(); 83 cout<<"第五种方法的输出:"<<endl; 84 cout<<"第五种方法数组的长度为:"<<size<<endl; 85 cout<<"下面这种方法是采用向量遍历的方法遍历数组:"<<endl; 86 for(vector<int>::iterator iter=begin_iter;iter!=end_iter;iter++) 87 { 88 cout<<*iter; 89 } 90 cout<<endl; 91 cout<<"下面这种方法是采用普通遍历数组的方法遍历数组:"<<endl; 92 for(int i=0;i<size-1;i++) 93 { 94 cout<<verc[i]; 95 } 96 cout<<endl; 97 }