C Online Compiler
Example: Array Sum using Pointers in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Array Sum using Pointers #include <stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50}; int n = sizeof(arr) / sizeof(arr[0]); int sum = 0; // Step 1: Declare a pointer and initialize it to the start of the array int *ptr = arr; // ptr now points to arr[0] // Step 2: Loop while the pointer is within the array bounds // We iterate 'n' times, incrementing the pointer each time for (int i = 0; i < n; i++) { sum += *ptr; // Add the value pointed to by ptr to sum ptr++; // Move pointer to the next element } printf("The sum of array elements using pointers is: %d\n", sum); return 0; }
Output
Clear
ADVERTISEMENTS