C Program to input and print array elements using pointer

#include <stdio.h>
#include<conio.h>
#define MAX_SIZE 100 // Maximum array size

int main()
{
     int arr[MAX_SIZE];
     int N, i;
     int * ptr = arr;    // Pointer to arr[0]

     printf("Enter size of array: ");
     scanf("%d", &N);

     printf("Enter elements in array:\n");
     for (i = 0; i < N; i++)
     {
          scanf("%d", ptr);

          // Move pointer to next array element
          ptr++;
     }

     // Make sure that pointer again points back to first array element
     ptr = arr;

    printf("Array elements: ");
     for (i = 0; i < N; i++)
    {
          // Print value pointed by the pointer
        printf("%d, ", *ptr);

        // Move pointer to next array element
          ptr++;
    }

     return 0;
     getch();
}

Run:


No comments:

Post a Comment

100 C Programs with Code and Output

Program 1: C Program To Read Two Numbers And Print The Sum Of Given Two Numbers. Program 2: C Program To Read Three Numbers And Prin...