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[0]; // Point to the first element's memory address
  6. // It is same as: int* ptr = array;
  7.  
  8. // Print out values of first two elements
  9. printf("First element value: %d\n", array[0]);
  10. printf("Second element value: %d\n", array[1]);
  11.  
  12. // Update values using pointer
  13. printf("Updating values of first two elements using pointer.\n");
  14. *ptr = 11; // Update value of first element
  15. ++ptr; // Move pointer to second element. To go back, you can use --ptr etc.
  16. *ptr = 21; // Update value of second element
  17.  
  18. // Print out updated values of first two elements
  19. printf("First element value: %d\n", array[0]);
  20. printf("Second element value: %d\n", array[1]);
  21.  
  22. // In this example, we didn't touch third and fourth values of array,
  23. // which are "30" and "40".
  24.  
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0s 2052KB
stdin
Standard input is empty
stdout
First element value: 10
Second element value: 20
Updating values of first two elements using pointer.
First element value: 11
Second element value: 21