Skip to content
Justice Tech School

Arrays and Pointers Together

An array name decays to a pointer to its first element. Indexing a[i] means *(a + i). When passed to a function the size is lost, so pass it too.

Example
#include <stdio.h>
int sum(const int *a, size_t n) { int t = 0; for (size_t i = 0; i < n; i++) t += a[i]; return t; }
int main(void) {
    int v[] = {1, 2, 3, 4};
    printf("%d %d %zu\n", v[2], *(v + 2), sizeof v / sizeof v[0]);
    printf("%d\n", sum(v, 4));
    return 0;
}

Result

3 3 4
10

Exercise

Pointer arithmetic form of a[2].

*(a + )

Quiz Test

Why pass the array size to a function?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.