C++ - Loop through an array using pointer
This program will loop through an array using pointer.
CODE
#include <iostream>
using namespace std;
int main()
{
int elements[] = { 10, 20, 30, 40, 50 };
//find the size of the array
int size = sizeof(elements) / sizeof(elements[0]);
//loop through the array using pointer and display the index and element
for (int *p = elements; p < (elements + size); p++) {
cout << (p-elements) << " : " << *p << endl;
}
return 0;
}
INPUT & OUTPUT
Comments
Post a Comment