Pointer notations to access array

Hi
This is the code I wrote. Im supposed to use pointer notations to access the array but I did without it. How can I use pnotations to access it instead of doing directly?
Thanks
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
using namespace std;


void inverseMat(int array[2][2], int d)
{
		int i, j, x, y;
	cout << "This is a matrix inverse for an A of size 2 x 2" << endl;
    cout << endl;
    cout << "Array A:     " << endl;
    cout << endl;
    
	for (i=0; i<2; i++)
    {
    	cout << "|";
    		for (j =0; j<2; j++)
    	{    
    		 cout <<" " <<  array[i][j];
    		 
		}
		cout << "|";
		cout << endl;
	}
    cout << endl;
    cout << "Determinant : " << endl;
    cout << d << endl;
    cout << endl;
  
}
int main()
{

    double det, inv, inv1, inv2, inv3, inv4;
	int A[2][2] = {8, -5, -3, 1};
	int i, j, x, y;
   	det = (A[0][0]*A[1][1]) - (A[0][2]*A[0][1]);

	inv =  1/det;
	inv1 = inv*A[0][0];
	inv2 = inv*A[0][1];
	inv3 = inv*A[0][2];
	inv4 = inv*A[1][1];

	double B[2][2] = {inv1, inv2, inv3, inv4};
	inverseMat(A,det);
	
	cout << "Array B or inverse of A :" << endl;
	for (x=0; x<2; x++)
    {
    	cout << "|";
    		for (y =0; y<2; y++)
    	{    
    		 cout <<" " <<  B[x][y];
		}
		cout << "|";
		cout << endl;
  
}
return 0;
}



Last edited on
array[i] is shorthand for the pointer arithmetic and subsequent dereference *(array+i)

array[i][j] is shorthand for the pointer arithmetic and subsequent dereference *(array + (i * row_length) + j)

Each time in your code you're accessing your array with the first, change that code to the second.
array[i][j] is actually equivalent to *(*(array + i) + j).
What he said. I must confess I haven't used an array for years and as a general rule see multi-dimensional arrays as potential design warnings. I am often bemused at seeing them taught to beginners.
Last edited on
Topic archived. No new replies allowed.