Write C Program input and print array elements using pointer


Introduction

I have used Code::blocks 12 compiler for debugging purpose. But you can use any C programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Write C Program input and print array elements using pointer


  1. #include <stdio.h>
  2. #define MAX_SIZE 100 // Maximum array size
  3.  
  4. int main()
  5. {
  6. int arr[MAX_SIZE];
  7. int num, i;
  8. int * ptr = arr; // Pointer to arr[0]
  9.  
  10. printf("Enter size of array: ");
  11. //Inputting size of the array from user
  12. scanf("%d", &num);
  13.  
  14. printf("Enter elements in array:\n");
  15. for (i = 0; i < num; i++)
  16. {
  17. scanf("%d", ptr);
  18. // Moving pointer to next array element
  19. ptr++;
  20. }
  21. ptr = arr;
  22.  
  23. printf("Entered array elements are: ");
  24. for (i = 0; i < num; i++)
  25. {
  26. // Print value pointed by the pointer
  27. printf("%d ", *ptr);
  28. // Move pointer to next array element
  29. ptr++;
  30. }
  31.  
  32. return 0;
  33. }
  34.  

Result

C language techstudy.org

     





Related Topics




Popular Posts