C - Use pointer notation to get to the values within a two-dimensional array

Introduction

To use pointer notation to get to the values within a two-dimensional array, use the indirection operator.

The following code changes the preceding example to display the value of the first element.

Demo

#include <stdio.h>

int main(void)
{
  char matrix[3][3] = {
    { '1','2','3' },
      { '4','5','6' },
      { '7','8','9' }
  };// www.j  a  v  a  2 s  .  c o m

  printf("value of matrix[0][0] : %c\n", matrix[0][0]);
  printf("value of *matrix[0]   : %c\n", *matrix[0]);
  printf("value of **matrix     : %c\n", **matrix);
  return 0;
}

Result

If we use matrix (array name) to get the value of the first element, apply two indirection operators to get it: **matrix.

If we use only one *, you will get the address of the first element of the array of arrays, which is the address referenced by matrix[0].

matrix refers to the address of the first element in the array of subarrays.

matrix[0], matrix[1], and matrix[2] refer to the addresses of the first element in each of the corresponding subarrays.

Using two index values accesses the value stored in an element of the array.

Related Topic