Q:

Write C Program input and print array elements using pointer

belongs to collection: C language Pointer Exercises

0

Write C Program input and print array elements using pointer

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size
 
int main()
{
    int arr[MAX_SIZE];
    int num, i;
    int * ptr = arr;    // Pointer to arr[0]
 
    printf("Enter size of array: ");
    //Inputting size of the array from user
    scanf("%d", &num);
 
    printf("Enter elements in array:\n");
    for (i = 0; i < num; i++)
    {
        scanf("%d", ptr);
        // Moving pointer to next array element
        ptr++;
    }
    ptr = arr;
 
    printf("Entered array elements are: ");
    for (i = 0; i < num; i++)
    {
        // Print value pointed by the pointer
        printf("%d ", *ptr);
        // Move pointer to next array element
        ptr++;
    }
 
    return 0;
}

Result:

Enter size of array: 5

Enter elements in array:

1

2

3

4

5

Entered array elements are: 1 2 3 4 5

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write C program to change the value of constant in... >>
<< Write C Program to add two numbers using pointers...