I am trying to access a two dimensional array in a program and also I am trying to use negative indexes (it helps me in mental steps). I wanted to use the neatest possible syntax for access array element viz a[i][j]
.
However when I run the program , I get segmentation fault.
#include <iostream>
int main (void)
{
int i,j;
int arr[3][3];
int ** p;
for( i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
arr[i][j] = i+j;
}
}
p = (int **)&(arr[1][1]);
for( i=-1; i<2; i++)
{
for(j=-1; j<2; j++)
{
std::cout << p[i][j] << std::endl;
}
}
return 0;
}
I don't want to use something like p[i*something + j]
for accessing array elements. Is it possible?