In this tutorial you will learn about the C Program to Access Elements of an Array Using Pointer and its application with practical example.
C Program to Access Elements of an Array Using Pointer
In this tutorial, we will learn to create a C program that will Access Elements of an Array Using Pointer using C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C Programming.
- Using pointer in C programming.
Access Elements of an Array Using Pointer:-
As we all know array is a collection of similar data type elements. In an array, only one variable is declared which can store multiple values. First will take the elements of an array from the user. Then will pass the elements address to the pointer and the will print the elements of array /Access Elements of an Array Using Pointer with the help of C Programming Language.
Pointer is a variable that can store the address of another variable.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
STEP 1: START STEP 2: INITIALIZE array[] STEP 3: take input elements STEP 4: create pointer STEP 5: pass array to pointer STEP 6: print array using pointer STEP 7: Return 0. STEP 8: END. |
Program:-
Access Elements of an Array Using Pointer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> int main() { //declaring variaable int arry[5]; int i; //Taking elements of array from the user printf("Enter 5 elements for the array: "); for (i = 0; i < 5; ++i) scanf("%d", arry + i); //printing elements of array with the help of pointer //declaring pointer int *ptr = arry; printf("You have entered \n"); for(i = 0; i < 5; i++) printf("array[%d] = %d\n",i,*(ptr+i)); return 0; } |
Output:-
The above program we have to first initialize the required variable.
- array = it will hold the elements in an array.
- i = it will hold the integer value to control array.
Creating Pointer.
- *ptr = it will hold the address of elements in a array.
Taking input from the user in an array elements in array.
Printing using array.