Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

Access 2d array with pointer C

#include <stdio.h>

int main(void) {
  
  // 2d array
  int num[3][4] = {
    {1, 2,  3,  4},
    {5, 6,  7,  8},
    {9, 10, 11, 12}
  };
  
  int
    ROWS = 3,
    COLS = 4,
    i, j;

  // pointer
  int *ptr = &num[0][0];
  
  // print the element of the array via pointer ptr
  for (i = 0; i < ROWS; i++) {
    for (j = 0; j < COLS; j++) {
      printf("%d ", *(ptr + i * COLS + j));
    }
    printf("
");
  }
  
  return 0;
}
Source by dyclassroom.com #
 
PREVIOUS NEXT
Tagged: #Access #array #pointer #C
ADD COMMENT
Topic
Name
4+4 =