I read how to pass 2-D arrays in a function as a parameter and tried to implement the same. There are two problems which I encountered:
1) The first line of the output of the code contains garbage value. 2) What does the line ((arr + in) + j) actually do ? I mean, Why can't we do something like ((arr + i) + j) to access arr[i][j] ?
I also tried passing the matrix using parameter int **arr and then tried printing the value as arr[i][j] but there was no output.
Here is the output that I get:-
Enter number of nodes: 4
0167772161878012032-1
0000
0000
0000
And here is my code :-
#include <iostream>
using namespace std;
void show(int* arr, int n)
{
int i, j;
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
cout << *((arr + i*n) + j);
}
cout << endl;
}
}
int main()
{
int n, i, j;
cout << "Enter number of nodes: ";
cin >> n;
int arr[n][n] = {{0}}; //Will initialize all elements in the matrix with 0.
show((int*)arr, n);
}