0

I know how to work pointers with a unidimensional array,(the identifier of the array points to its first element)..., but i would like to know how can i work the same with pointers but using arrays with more than one dimension?

Daniel Conde Marin
  • 7,588
  • 4
  • 35
  • 44

1 Answers1

1

The same:

int a[10]; // array(10) of ints
int* pa = a; // pa points to first int
pa++; // pa points to second int

int b[10][20]; // array(10) of arrays(20) of ints
int (*pb)[20] = b; // pb points to first array(20) of ints
pb++; // pb points to second array(20) of ints

If you want to traverse 2-dimensionally, use manual indexing:

int tab[W][H];
int* p = &tab[x+y*W]; // element (x,y)
p += 1; // one element forward (x+1)
p += W; // one column forward (y+1)
Kos
  • 70,399
  • 25
  • 169
  • 233