C Online Compiler
Example: Access Array Elements by Incrementing Pointer in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Access Array Elements by Incrementing Pointer #include <stdio.h> int main() { // Step 1: Declare and initialize an array int scores[] = {10, 20, 30, 40, 50}; int size = sizeof(scores) / sizeof(scores[0]); // Step 2: Declare a pointer and point it to the first element of the array int *p = scores; // Step 3: Iterate and access elements by incrementing the pointer printf("Accessing elements by incrementing the pointer:\n"); for (int i = 0; i < size; i++) { printf("Element %d: %d\n", i, *p); // Access current element p++; // Move pointer to the next element } // Note: 'p' now points one past the end of the array. // To re-access from start, you'd need to re-initialize p = scores; return 0; }
Output
Clear
ADVERTISEMENTS