
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Access Elements of an Array Using Pointer
Pointers store the memory location or address of variables. In other words, pointers reference a memory location and obtaining the value stored at that memory location is known as dereferencing the pointer.
A program that uses pointers to access a single element of an array is given as follows −
Example
#include <iostream> using namespace std; int main() { int arr[5] = {5, 2, 9, 4, 1}; int *ptr = &arr[2]; cout<<"The value in the second index of the array is: "<< *ptr; return 0; }
Output
The value in the second index of the array is: 9
In the above program, the pointer ptr stores the address of the element at the third index in the array i.e 9.
This is shown in the following code snippet.
int *ptr = &arr[2];
The pointer is dereferenced and the value 9 is displayed by using the indirection (*) operator. This is demonstrated as follows.
cout<<"The value in the second index of the array is: "<< *ptr;
Another program in which a single pointer is used to access all the elements of the array is given as follows.
Example
#include <iostream> using namespace std; int main() { int arr[5] = {1, 2, 3, 4, 5}; int *ptr = &arr[0]; cout<<"The values in the array are: "; for(int i = 0; i < 5; i++) { cout<< *ptr <<" "; ptr++; } return 0; }
Output
The values in the array are: 1 2 3 4 5
In the above program, the pointer ptr stores the address of the first element of the array. This is done as follows.
int *ptr = &arr[0];
After this, a for loop is used to dereference the pointer and print all the elements in the array. The pointer is incremented in each iteration of the loop i.e at each loop iteration, the pointer points to the next element of the array. Then that array value is printed. This can be seen in the following code snippet.
for(int i = 0; i < 5; i++) { cout<< *ptr <<" "; ptr++; }
- Related Questions & Answers
- How to access elements of an array using pointer notation in C#?
- How to access array elements using a pointer in C#?
- How to access elements from an array in C#?
- Pointer to an Array in C
- C# Program to access tuple elements
- C program to reverse an array elements
- Sum of array using pointer arithmetic in C++
- Sum of array using pointer arithmetic in C
- Explain the concept of Array of Pointer and Pointer to Pointer in C programming
- C++ Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
- An Uncommon representation of array elements in C++ program
- C Program to find sum of perfect square elements in an array using pointers.
- Rank of All Elements in an Array using C++
- How to access elements from jagged array in C#?
- C# Program to skip elements of an array from the end