fork download
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. int array[4] = {10, 20, 30, 40}; // Initial values in array
  5. int* ptr = &array[3]; // Point to the fourth element's memory address
  6.  
  7. // Print out values of last two elements
  8. printf("Third element value: %d\n", array[2]);
  9. printf("Fourth element value: %d\n", array[3]);
  10.  
  11. // Update values using pointer
  12. printf("Updating values of last two elements using pointer.\n");
  13. *ptr = 41; // Update value of fourth element
  14. --ptr; // Move pointer to third element.
  15. *ptr = 31; // Update value of third element
  16.  
  17. // Print out update values of last two elements
  18. printf("Third element value: %d\n", array[2]);
  19. printf("Fourth element value: %d\n", array[3]);
  20.  
  21. // In this example, we didn't touch first and second elements' values of array,
  22. // which are "10" and "20".
  23.  
  24. return 0;
  25. }
  26.  
Success #stdin #stdout 0s 2052KB
stdin
Standard input is empty
stdout
Third element value: 30
Fourth element value: 40
Updating values of last two elements using pointer.
Third element value: 31
Fourth element value: 41