logo

Iterating over an array using pointers

#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char* argv[]) { int arr[] = { 7, 8, 9, 10, 11 }; int n = 5; printf("Start address: %llu\nEnd address: %llu\n", &arr[0], &arr[4]); // Iterating using index int i; for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); // Iterating using a pointer int *p; for (p = &arr[0]; p <= &arr[4]; p++) { printf("%d ", *p); } return 0; }

Courses with this lesson