fork download
  1. #include <stdio.h>
  2.  
  3. #define SIZE 5
  4.  
  5. int main(void)
  6. {
  7. int values[SIZE] =
  8. { 2, 4, 6, 8, 10 };
  9.  
  10. int *vPtr;
  11.  
  12. printf("Using array subscript notation:\n");
  13.  
  14. for (int i = 0; i < SIZE; ++i)
  15. {
  16. printf("%d\t", values[i]);
  17. }
  18.  
  19. puts("");
  20.  
  21. vPtr = values;
  22.  
  23. printf("\nUsing pointer/offset notation:\n");
  24.  
  25. for (int i = 0; i < SIZE; ++i)
  26. {
  27. printf("%d\t", *(vPtr + i));
  28. }
  29.  
  30. puts("");
  31.  
  32. printf("\nUsing pointer/offset notation with array name:\n");
  33.  
  34. for (int i = 0; i < SIZE; ++i)
  35. {
  36. printf("%d\t", *(values + i));
  37. }
  38.  
  39. puts("");
  40.  
  41. printf("\nUsing pointer/subscript notation:\n");
  42.  
  43. for (int i = 0; i < SIZE; ++i)
  44. {
  45. printf("%d\t", vPtr[i]);
  46. }
  47.  
  48. puts("");
  49.  
  50. printf("\nElement 4 using values[4] = %d\n",
  51. values[4]);
  52.  
  53. printf("Element 4 using *(values + 4) = %d\n",
  54. *(values + 4));
  55.  
  56. printf("Element 4 using vPtr[4] = %d\n",
  57. vPtr[4]);
  58.  
  59. printf("Element 4 using *(vPtr + 4) = %d\n",
  60. *(vPtr + 4));
  61.  
  62. printf("\nAddress referenced by vPtr + 3: %p\n",
  63. (void *)(vPtr + 3));
  64.  
  65. printf("Value stored at that address: %d\n",
  66. *(vPtr + 3));
  67.  
  68. vPtr = &values[4];
  69.  
  70. vPtr -= 4;
  71.  
  72. printf("\nAddress after vPtr -= 4: %p\n",
  73. (void *)vPtr);
  74.  
  75. printf("Value stored at that address: %d\n",
  76. *vPtr);
  77.  
  78. return 0;
  79. }
Success #stdin #stdout 0s 5328KB
stdin
Standard input is empty
stdout
Using array subscript notation:
2	4	6	8	10	

Using pointer/offset notation:
2	4	6	8	10	

Using pointer/offset notation with array name:
2	4	6	8	10	

Using pointer/subscript notation:
2	4	6	8	10	

Element 4 using values[4] = 10
Element 4 using *(values + 4) = 10
Element 4 using vPtr[4] = 10
Element 4 using *(vPtr + 4) = 10

Address referenced by vPtr + 3: 0x7ffce84f130c
Value stored at that address: 8

Address after vPtr -= 4: 0x7ffce84f1300
Value stored at that address: 2