Hi it's my first time posting in this forum and a new member here in this site. I'm testing a little program fragment for my homework and my objective is to pass a 2d-array with a user-defined size into a function. i figured out that using a pointer in passing a 2d-array is easier (since the size is user-defined)
Note:I assume that users can change the txt file freely, but this program does not display an error prompt when the user failed to match the array size and the contents of the array (i will work on that later)
please read the program please before i ask my question.
first of all... here's the content of my sample file "matrixA.txt"
3 5
2 4 6 8 10
3 5 7 9 11
16 15 14 13 12
|
then here's my program
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 34 35 36 37 38
|
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
void dispMtrx(int a, int b, int *ptr){
for(int x=0; x<a; x++){
for(int y=0; y<b; y++){
cout << *ptr << " ";
cout << endl;
}
ptr++;
}
}
int main(){
int r, c, r2, c2, row, col;
ifstream fin;
fin.open("matrixA.txt");
fin >> r >> c;
int mtrxA[r][c];
int *arrPtr;
arrPtr = mtrxA[][c];
while(!fin.eof()){
for(int x=0; x<r; x++){
for(int y=0; y<c; y++)
fin >> mtrxA[x][y];
}
}
dispMtrx(r, c, arrPtr);
cout << endl;
system("pause");
}
|
after i compile it, le wild error appears
expected primary-expression before ']' token (in line 26, col 16)
|
my hypothesis is that there's something wrong with my declaration of 2d-array in line 26. I checked it but i really have no idea what's wrong.
is there really something wrong with my 2d-array declaration? if none, where's my unseen mistake?
pls help me... and tnx in advance