0

I am trying to input values into a 3x3 matrix using pointers instead of using index. Below is my main program:

int main() {
 int mat[ROW][COL];
 int *p = &mat[0][0];

 minput(p, ROW, COL);

 return 0; 
}

And this is my minput function:

void minput(int *m, int row, int col) {
  // 1 2 3
  // 4 5 6
  // 7 8 9

 *(m+0) = 1;

}

What I cant figure out is how to input the rest of the matrix, like [1][1]. I tried *(*(p+1)+1) to try and access m[1][1] but it didnt seem to work. I hope this is clear.

l00kitsjake
  • 955
  • 3
  • 11
  • 24

1 Answers1

2

In the minput function, you can access mat[i][j] by *(m+i*col+j)

Also check In C, are arrays pointers or used as pointers?, very useful

Community
  • 1
  • 1
justmscs
  • 756
  • 4
  • 12