Reputation: 149
I have created a matrix with pointer to pointers.
int** matrix = new int*[5];
for(int i = 0; i < 5; i++)
matrix[i] = new int[5];
If I assume correctly, what this does is it creates a pointer named matrix, which points to an array of pointers, and each pointer in the array points to an integer array, which array's items consist of integer type data. I have written in number this way:
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
matrix[i][j] = 6 + j;
And here comes my problem: when I try to compare items from my matrix, the if statement does not compare the data stored inside the matrix, but it compares the items' memory addresses.
if(matrix[i][0] == matrix[i][j])
How can I solve this problem? I have tried a lot of different things, but none has worked so far. Thanks in advance!
Upvotes: 0
Views: 51
Reputation: 1877
You cannot dereference any further, as matrix[i][j]
resolves to the actual value (the integer) inside the matrix.
I tested with this code:
cout << "Value of matrix[0][0]: " << matrix[0][0] << endl;
cout << "Pointer to matrix[0][0]: " << &matrix[0][0] << endl;
Which gave me:
Value of matrix[0][0]: 6
Pointer to matrix[0][0]: 0x1705040
Upvotes: 2