C - Pointers for array notation

Introduction

Array notation can easily be replaced by pointer notation.

The following table compares array notation with pointer notation.

Assume that pointer a is initialized to array alpha.

The array and pointer must be the same variable type.

Array alpha[] Pointer a
alpha[0]*a
alpha[1]*(a+1)
alpha[2]*(a+2)
alpha[3]*(a+3)
alpha[n]*(a+n)

You can test your knowledge of array-to-pointer notation by using a sample program.

Demo

#include <stdio.h> 

int main()/*from  w ww .j  a v  a2 s.c o  m*/
{
  float temps[5] = { 1.1, 2.2, 3.3, 4.4, 5.5 };

  printf("The temperature on Tuesday will be %.1f\n", temps[1]);
  printf("The temperature on Friday will be %.1f\n", temps[4]);


  printf("The temperature on Tuesday will be %.1f\n", *(temps + 1));
  printf("The temperature on Friday will be %.1f\n", *(temps + 4));


  float* t = temps;
  printf("The temperature on Tuesday will be %.1f\n", *(t + 1));
  printf("The temperature on Friday will be %.1f\n", *(t + 4));

  return(0);
}

Result

Related Topic