C - Incrementing a pointer to an array of long type integers

Introduction

The compiler presumes that when you add 1 to an address value, what you actually want to do is access the next variable of that type.

Demo

#include <stdio.h>

int main(void)
{
  long multiple[] = {15L, 25L, 35L, 45L};
  long *p = multiple;

  for(int i = 0 ; i < sizeof(multiple)/sizeof(multiple[0]) ; ++i)
    printf("address p+%d (&multiple[%d]): %llu        *(p+%d)   value: %d\n",
                              i, i, (unsigned long long)(p+i),  i,  *(p+i));

  printf("\n    Type long occupies: %d bytes\n", (int)sizeof(long));
  return 0;/*w  w  w.  j  ava 2s .co m*/
}

Result

Related Topic