c - Accessing 2D array elements using double pointer -
recently had interview in c
. interviewer has asked me explain how access particular element in 2d array
using double pointer
. gave answer *(*(a+i)+j)
, a
double pointer, i
number of rows , j
number of columns. later asked me explain using example. confused @ *(a+i)
gives value instead of address , adding j
gives junk value. can please explain.
remember in 1d array a[i]
equals *(a+i)
. , there no 2d arrays in c, arrays of arrays.
so a[i][j]
equal *(*(a+i)+j)
.
if type of a
int**
, type of (a+i)
still int**
, need dereference it. type of *(a+i)
int*
, type of *(*(a+i)+j)
int
.
about interview question, no matter a
double pointer, should still use []
notation. alternative cumbersome:
int **a = ...; int x = a[i][j];
Comments
Post a Comment