C Online Compiler
Example: Access Array Elements using Pointer Arithmetic in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Access Array Elements using Pointer Arithmetic #include <stdio.h> int main() { // Step 1: Declare and initialize an array int numbers[] = {100, 200, 300, 400, 500}; int size = sizeof(numbers) / sizeof(numbers[0]); // Step 2: Declare a pointer and point it to the first element of the array int *ptr = numbers; // 'numbers' is equivalent to &numbers[0] // Step 3: Access elements using pointer arithmetic printf("Accessing elements using pointer arithmetic:\n"); for (int i = 0; i < size; i++) { printf("Element at index %d: %d\n", i, *(ptr + i)); } return 0; }
Output
Clear
ADVERTISEMENTS