2d-array syntax problem

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
The correct syntax is:

arrPtr = mtrxA[c]; // No empty []


Line 24 is an extension some compiler support, but not legal standard C++. In standard C++ the dimensions of an array have to be const
huh? i tried changing it to this...

arrPtr = mtrxA[][5];


but it didn't work :(

im pretty sure that in declaring a 2d-array, it's ok to leave the row part blank but it's not ok to leave the column part blank, right?
Last edited on
It's not ok to leave any of it blank.
aw... so it should be like this?
arrPtr = mtrxA[3][5];


but considering my objectives, i want to make it like this
arrPtr = mtrx[r][c]; // r for user-defined row, and c for user-defined column
... but then again, i tried it a lot and it didn't work as well

does that mean... my objective is a "mission impossible" (since the rows and column must be constants and cannot be user-defined anymore)

but... what situation will it be ok for the 1st bracket to be left empty? (is it only for parameter passing and not for declaring?)
The best thing for your situation is std::vector.

Try Googling C++ Vector or reading the stuff about it on this website. It offers dynamic sized arrays along with a bunch of useful functions you can do on them.
Last edited on
huh? i tried changing it to this...
Line 24 must be const

Line 26: To get a row according to a column you need to write

arrPtr = mtrxA[c]; // No empty []

Oh, and c is in this context out of range
Last edited on
hmmm... I guess there's no other choice then... i'll go back to pre-defined 2d-arrays :3

man, user-defined 2d-arrays are not so easy huh? i can't reach my optional objectives yet XD haha
Last edited on
std::vector is a super easy way to use dynamically sized arrays. The entire thing is already implemented for you...
oh... ok i found it! :D gotta do some studying here...
Topic archived. No new replies allowed.