2D Dynamic array

Here is my code that I have written. It is suppose to take values from a .dat file. the first and second value contain the number of rows and columns and the other numbers represent the position and values of the numbers going into an array.

I get an error that says: 1>c:\users\zoheb\documents\visual studio 2008\projects\dynamic 2d array\dynamic 2d array\dynamic 2d array.cpp(20) : error C2440: '=' : cannot convert from 'int *' to 'int'

#include <iostream>
#include <fstream>

using namespace std;

int main () {

int height, width;
int sum,row,column,value;
int **array;

ifstream inFile("matrix1.dat");

if(!inFile) {
cerr << "File matrix1.dat could not be opened!";
}

inFile >> height >> width;
cout << height << ' '<< width << endl;
**array = &array[height][width];

while(inFile) {
inFile >> row >> column >> value;
array[row][column] = value;
}

//calculate the sum of each row

for (int i=0; i<height; i++) {
sum = 0;
for (int j=0; j<width; j++) {
sum += array[i][j];
}
cout << i << " = " << sum << endl;
}

//calculate the sum of each column

for (int i = 0; i < width; i++) {
sum = 0;
for (int j = 0; j < height; j++) {
sum += array[j][i];
}
cout << i << " = " << sum << endl;
}
for (int i = 0; i < height; i++){
delete[] array[i];
delete[] array;
}

}
You didn't allocate an array of pointers for 'array' to point to. This will cause run-time errors.
'array' is of type 'int **'. array[height] is of type 'int *'. array[height][width] is of type 'int'. By dereferencing it, you're asking for an 'int *', but **array (or array[0][0]) is an int.
By the way, when you need to dereference something like this, it's faster to do array[x]+y.
Topic archived. No new replies allowed.