Display the contents of an array using pointer arithmetic - C++ Data Type

C++ examples for Data Type:Array Pointer

Description

Display the contents of an array using pointer arithmetic

Demo Code

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

void displayArray(int intArray[], int nSize){
    cout << "The value of the array is:\n";

    int* pArray = intArray;

    for(int n = 0; n < nSize; n++, pArray++){
        cout << n << ": " << *pArray << "\n";
    }//from   w  w w.  j  av  a  2s . c o m
    cout << endl;
}

int main(int nNumberofArgs, char* pszArgs[])
{
    int array[] = {4, 3, 2, 1};
    displayArray(array, 4);

    return 0;
}

Result


Related Tutorials