This error occurred during program compilation today:
An array is defined in the header file:
View code
1 char s[]="1234567890";
A function of the current display group is defined:
View code
1 void Display(char* c);
The following two statements show the size of the realistic array in the real function and the main function respectively:
View code
1 sizeof(c);
2 sizeof(s);
The actual results are quite different, with 11 in the main function and 4 in the real function.
After thinking, we found that in the main function S represents an array, while in the Real Function C represents a pointer to the array header. The array size is 11 and the pointer size is 4.
The main program is as follows:
View code
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;
11 cout <"this is a test of the array length in the main function:" <sizeof (s) <Endl;
12 display (s );
13 CIN>;
14}
15
16
17 void display (char * C)
18 {
19 cout <"this is a test of the array length in the subfunction:" <sizeof (c) <Endl;
20}
Realistic results:
My question is, how can I refer to an array in both the primary and subfunctions, instead of a pointer to the array header ???
On the Internet, I checked several methods for passing arrays as function parameters, as shown below:
View code
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 <"this is a test of the array length in the main function:" <sizeof (s) <Endl;
20 display (s );
21 cout <"*********************************** * ********* "<Endl;
22 putarray1 (A, 10 );
23 putarray2 (A, 10 );
24 putarray3 ();
25 putarray4 (B );
26 CIN> q;
27}
28
29
30 void display (char * C)
31 {
32 cout <"this is a test of the array length in the subfunction:" <sizeof (c) <Endl;
33}
34 void putarray1 (int * P, int length1)
35 {
36 int length1 = sizeof (P );
37 cout <"output of the first method:" <Endl;
38 cout <"the length of the first method array is:" <leng22. <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 length1 = sizeof (P );
48 cout <"output of the second method:" <Endl;
49 cout <"the length of the second method array is:" <length1 <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 length1 = sizeof (P );
59 cout <"output of the third method:" <Endl;
60 cout <"the length of the third method array is:" <length1 <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 length1 = sizeof (P );
70 cout <"output of the fourth method:" <Endl;
71 cout <"The fourth method array is long:" <length1 <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 <"output of the fifth method:" <Endl;
84 cout <"The Fifth method array is long:" <size <Endl;
85 cout <"the following method uses vector traversal to traverse the array:" <Endl;
86 for (vector <int >:: iterator iter = begin_iter; iter! = End_iter; ITER ++)
87 {
88 cout <* ITER;
89}
90 cout <Endl;
91 cout <"the following method traverses an array using a common traversal array:" <Endl;
92 for (INT I = 0; I <size-1; I ++)
93 {
94 cout <verc [I];
95}
96 cout <Endl;
97}
Here, int * arr and INT arr [] have the same meaning. The Compiler automatically replaces int arr [] with int * arr, therefore, this explains the reason why the above main function and subfunction obtain different results by using the array name for Array length. This happens only when the array is passed as a function parameter (C ++ primer plus, p192 ).
The fourth method is not understood. Please stay here for the moment.
In addition, although the above four methods can correctly pass arrays as parameters in sub-functions, they still cannot meet the requirements of the blog at the beginning: the length of the parameter array that can be tested in the subfunction. Later, I checked C ++ primer plus and found that the book clearly pointed out that this method was not implemented. The length of the array must be passed as a parameter in the function.
In addition, because the fifth method needs to redefine the Vector Template, which is inconsistent with the topic, it is not called in the main function. The program in the routine is as follows:
View code
1 vector<int> verc1(a,a+10);
2 vector<int> verc2(b,b+8);
3 PutArray5(verc1);
4 PutArray5(verc2);
The above five methods to call arrays are only different in the way of passing arrays. They can be summarized as a way to pass arrays, that is, to pass the pointer to the array header and the length of the array. Another way to pass an array is to pass the pointer pointing to the array header and the pointer pointing to the end of the array as two parameters. The function is defined as follows:
View code
1 int sum_arr(const int* begin,const int* end);
I have some feelings when learning this knowledge point. Although C ++ primer has read it again, the knowledge points in it have been carefully digested, but when it comes to use, it won't work hard. It seems that you should practice the code on the book while reading the book, so that you can have a better understanding of the application of the Code.