Passing 2d array to a function help me please :)

Jan 31, 2014 at 7:40pm
Hello would you please be so kind to help me with my code. I should have written a function which outputs 2d array. However, I have a an error in this part :



f1(c,b,a[][c]);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  #include <iostream>
#include <string>
using namespace std;
void f1(int c,int a[][c],int b);
int main()
{
    int b,c;
    cout<<"Ramdnei ricxvi gindat ro masivshi ikos?";
    cin>>b>>c;
    int a[b][c];
    
    for (int i=0;i<b;i++){
        
        for(int j=0;j<c;j++){
        
        cin>>a[i][j];
        }
    }
    f1(c,b,a[][c]);
    
}


void f1(int b,int c,int a[][c]){
    
    for (int i=0;i<b;i++){
        
        for (int j=0;j<c;j++){
        cout<<a[i][j]<<endl;
        }
    }
    
}
Last edited on Jan 31, 2014 at 7:54pm
Jan 31, 2014 at 8:39pm
First, yo must pass arguments in the same order that the function takes them. Next, you only need to give the names of things as argument, and the prototype must match the actual function itself too...

(I don't know why you are mixing up a, b, and c.)


Btw, you are aware that VLAs are not legal in C++, right? And even if they were, you cannot specify the size of a VLA as argument to a function...

Some reading that might help
http://www.cplusplus.com/forum/general/49079/
http://www.cplusplus.com/forum/beginner/120441/#msg655547
And some more in-depth reading
http://www.cplusplus.com/forum/beginner/2967/#msg12100

Hope this helps.
Jan 31, 2014 at 9:14pm
I do not get it, please if you know how should I have written the error line correct me, because I think that everything other is correct. and what does VLA mean?
Jan 31, 2014 at 9:37pm
Variable Length Arrays

1
2
3
int n;
cin >> n;
int xs[ n ];  // xs is a variable-length array 

There is nothing to "get" besides learning the subject matter. Read the stuff given to you.

Good luck!
Topic archived. No new replies allowed.