0

I am trying to code for Spiral Matrix where the input is a 2D array. When trying to simulate the code using double pointer i get 'Access Violation Reading' exception. Referred couple of links and tried different syntax but exception is not resolved. link 1 link 2

This is the exact code snippet which is throwing exception when debugging in VS2015.

void SpiralMatrix(int **arr, int row, int col)
{
    int u = 0, l = 0, r = col-1, d = row - 1;
    int val = arr[0][0];// *(*(arr + 0) + 0); // Throwing exception at this location

    printf("%d",val);
}


int main()
{
    int arr[2][3] = { {1,2,3},{4,5,6} };
    SpiralMatrix((int **)arr,2,3);
}

I am really getting confused what am i missing here? I am trying to understand the root cause rather than the solution.

Community
  • 1
  • 1
SPB
  • 4,040
  • 16
  • 49
  • 62
  • 1
    You can't cast arr to (int **) – drescherjm Dec 28 '16 at 02:25
  • Remove the `(int **)` from the calling statement in `main()` This will cause the compiler to diagnose the problem (the `(int **)` forces the compiler to shut up, and accept a conversion it would otherwise reject). – Peter Dec 28 '16 at 02:30
  • 1
    arr is really `int*` but not `int**` you can use `SpiralMatrix((int *)arr,2,3);` say. in your case `arr[0]` is 1 - so `arr[0][0]` is equal `*(int*)1` - of course exception – RbMm Dec 28 '16 at 02:37

0 Answers0