echo.zyw
May 3rd, 2012, 06:12 PM
Hey guys,
I have a question about the following code. Any advice for me? Thanks in advance. :)
Code:
2 #include <iostream>
3 using namespace std;
4
5 int printarray(int (&array)[4],n){
6 cout << "Outputing from printarray func. " << endl;
7 for (int i = 0; i < 4; i++){
8 cout << array[i] << endl;
9 }
10 return n;
11 }
12
13 int main(){
14 int b[] = {1, 2, 3, 4};
15 int *a;
16 a = b;
17
18 cout << printarray(b,5) << endl;
19 return 0;
20 }
g++ -o a a.ccp error:
a.cpp:5:22: error: array was not declared in this scope
a.cpp:5:32: error: n was not declared in this scope
a.cpp:5:33: error: expression list treated as compound expression in initializer [-fpermissive]
a.cpp:5:34: error: expected , or ; before { token
But if I write function printarray(int (&array)[4],n) as printarray(int (&array)[4]), then it will work well for me. Any idea why the above error happened?
I have a question about the following code. Any advice for me? Thanks in advance. :)
Code:
2 #include <iostream>
3 using namespace std;
4
5 int printarray(int (&array)[4],n){
6 cout << "Outputing from printarray func. " << endl;
7 for (int i = 0; i < 4; i++){
8 cout << array[i] << endl;
9 }
10 return n;
11 }
12
13 int main(){
14 int b[] = {1, 2, 3, 4};
15 int *a;
16 a = b;
17
18 cout << printarray(b,5) << endl;
19 return 0;
20 }
g++ -o a a.ccp error:
a.cpp:5:22: error: array was not declared in this scope
a.cpp:5:32: error: n was not declared in this scope
a.cpp:5:33: error: expression list treated as compound expression in initializer [-fpermissive]
a.cpp:5:34: error: expected , or ; before { token
But if I write function printarray(int (&array)[4],n) as printarray(int (&array)[4]), then it will work well for me. Any idea why the above error happened?