C++ Online Compiler
Example: Pointer Increment Traversal in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Pointer Increment Traversal #include <iostream> using namespace std; int main() { // Step 1: Declare and initialize an integer array int numbers[] = {10, 20, 30, 40, 50}; int size = sizeof(numbers) / sizeof(numbers[0]); // Step 2: Declare an integer pointer and initialize it to point to the first element int* currentPtr = numbers; // Step 3: Iterate through the array by incrementing the pointer cout << "Accessing elements by incrementing a pointer:" << endl; for (int i = 0; i < size; ++i) { cout << "Element " << i << ": " << *currentPtr << endl; // Dereference currentPtr to get the value currentPtr++; // Increment currentPtr to point to the next element } return 0; }
Output
Clear
ADVERTISEMENTS