- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Environmental Science
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a C program using pointers to read in an array of integers and print its elements in reverse order.
#include <stdio.h>
#include <conio.h>
#define MAX 30
void main() {
int size, i, arr[MAX];
int *ptr;
clrscr();
ptr = &arr[0];
printf("\nEnter the size of array : ");
scanf("%d", &size);
printf("\nEnter %d integers into array: ", size);
for (i = 0; i < size; i++) { scanf("%d", ptr); ptr++; } ptr = &arr[size - 1]; printf("\nElements of array in reverse order are :"); for (i = size - 1; i >= 0; i--) {
printf("\nElement%d is %d : ", i, *ptr);
ptr--;
}
getch();
}
Output :
Enter the size of array : 5
Enter 5 integers into array : 11 22 33 44 55
Elements of array in reverse order are :
Element 4 is : 55
Element 4 is : 44
Element 4 is : 33
Element 4 is : 22
Element 4 is : 11
Program to read integers into an array and reversing them using pointers
Explanation :
1. We have declared one pointer variable and one array.
int size,i,arr[MAX];
int *ptr;
2. Address of first element of array is stored inside pointer variable.
ptr=&arr[0];
3. Accept Size of an Array.
printf("Enter the size of array : ");
scanf("%d",&size);
4. Now we have accepted element one by one using for loop and scanf statement.
printf("\nEnter %d integers into array: ", size);
for (i = 0; i < size; i++) {
scanf("%d", ptr);
ptr++;
}
5. Increment pointer variable so that it will then point to next element of array.
After accepting all elements store address of last element inside pointer variable.
ptr=&arr[size-1];
6. Again using reverse for loop and printf statement print an array.
for(i=size-1;i>=0;i--) {
printf("nElement%d is %d :",i,*ptr);
ptr--;
}
- Related Questions & Answers
- How to calculate gross salary of a person using C Program
- How to find sum of two numbers using C Language
- How to check the type of triangle using C
- How to Solve Second Order Quadratic Equation Using C Program
- Write C Program to Calculate Addition of All Elements in Array
- Write C Program to Find Smallest Element in Array in C Programming
- Write C Program to Find Factorial of Number Using Recursion
- Recursive program to linearly search an element in a given array using C
- How to Check Whether Number is Perfect Or Not using C
- How to reads customer number and power consumed and prints amount to be paid using C
- How to read the values of x, y and z and print the results expressions in one line using C Program
- Write C Program to Merge Two arrays in C Programming
- Find Second largest element in an array using C Programming
- Write a ‘C’ Program to compute the sum of all elements stored in an array using pointers
- How to reverse a given number using C
- What are Python function attributes?
Advertisements
ads