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

Using pointer/offset notation with nPtr:
0.0	1.1	2.2	3.3	4.4	5.5	6.6	7.7	8.8	9.9	

Using pointer/offset notation with array name:
0.0	1.1	2.2	3.3	4.4	5.5	6.6	7.7	8.8	9.9	

Using pointer/subscript notation:
0.0	1.1	2.2	3.3	4.4	5.5	6.6	7.7	8.8	9.9	

Element 4 using array subscript notation: 4.4
Element 4 using pointer/offset notation with array name: 4.4
Element 4 using pointer/subscript notation: 4.4
Element 4 using pointer/offset notation with nPtr: 4.4

Address referenced by nPtr + 8: 0x7ffc4dabcbd0
Value stored at that address: 8.8

Address after nPtr -= 4: 0x7ffc4dabcbb4
Value stored at that address: 1.1