Program to Traverse array using pointer

0 Suresh Chand April 1, 2021

In this program, we will learn to traverse array using pointer. It is simple pointer example. If you are beginner in pointer then check the pointer tutorials before learning this example

In this example, data are stored in variable data[].

Then we will access the elements using pointer notation. On thing you must know in pointer

  • arr[0] is equal to *arr and &arr[0] is equal to arr
  • arr[1] is equal to (*arr + 1) and &arr[1] is equal to arr + 1
  • ….
  • arr[i] is equal to (*arr + i) and &arr[i] is equal to arr + i

To know more about relationship, Check tutorial [Relationship between Array and Pointer]

Example: Traverse array using pointer

#include <iostream>

using namespace std;

int main()
{

    int *ip;
    int arr[] = {1, 2, 3, 4, 5, 6};
    ip = arr;

    for (int x = 0; x < 6; x++)
    {
        cout << *ip << endl;
        ip++;
    }

    return 0;
    
}

The output of above program is

Traverse array using pointer
Output
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Join Our Newsletter Now

Get the latest tutorials, examples and projects updates.

!

Ads

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Disable Ad Block

Want Same System