Array Size in C with sizeof: Formula and Pointer Trap

Learn the array-length idiom through a five-element C program, then trace the pointer adjustment that makes the same expression fail inside a function.

KnowledgeGate Team

Exam prep & CS education

Updated 10 Nov 20265 min read
Array Size in C with sizeof: Formula and Pointer Trap

sizeof array / sizeof array[0] gives the correct element count where the object is still an array. Move the same-looking expression into a function parameter, however, and it can produce 2 instead of 5. Whole-array bytes divided by one element's bytes produce the count; parameter adjustment replaces the array object with a pointer. The broader CS Fundamentals for Exams and Placements path can help you place this rule alongside related C concepts.

What sizeof measures for an array

In C, sizeof reports a size in bytes. Its result has type size_t. It does not directly report how many elements an array contains.

Suppose marks is an array. While marks still names that complete array object:

  • sizeof marks measures the bytes occupied by the whole array.

  • sizeof marks[0] measures the bytes occupied by one element.

The units explain the formula: (bytes in the whole array) / (bytes in one element) = number of elements.

Using the element expression is better than writing sizeof(int). If the element type later changes, sizeof marks[0] changes with it. For the first-pass memory model, 2D address arithmetic, and declaration traps, read the earlier Arrays and Pointers in C: Complete Guide with Worked Examples for GATE and Interviews. This post isolates the sizeof byte ratio and its function-parameter failure.

The formula, its valid scope, and a reusable local idiom

The main idiom is:

c
size_t count = sizeof marks / sizeof marks[0];

One condition must be true before you use it: marks must name an actual array at that point. It must not be a pointer or an array-shaped function parameter.

You may also write the type-resistant form:

c
size_t count = sizeof marks / sizeof *marks;

Both forms measure the same element type. The marks[0] version is often easier to read because the complete array and one indexed element appear side by side.

This works from information available about the object at that point in the source. An ordinary C array does not carry hidden runtime metadata from which code can recover its length after only a pointer to the first element remains.

Worked example: five marks, 20 bytes, count 5

Here is the complete program:

c
#include <stdio.h>
#include <stddef.h>

int main(void) {
    int marks[] = {72, 81, 90, 64, 88};
    size_t total_bytes = sizeof marks;
    size_t element_bytes = sizeof marks[0];
    size_t count = total_bytes / element_bytes;

    printf("%zu\n", count);
    return 0;
}

Now use an explicitly labelled example implementation where sizeof(int) == 4:

  1. The array contains five int elements.

  2. Five elements occupy 5 x 4 = 20 bytes.

  3. Therefore, sizeof marks == 20.

  4. One element occupies four bytes, so sizeof marks[0] == 4.

  5. The count is 20 / 4 == 5.

The program prints 5. C does not require an int to occupy four bytes. The ratio still returns five on another conforming implementation because the numerator and denominator use that same implementation's sizes.

Five-element marks array with values 72, 81, 90, 64, and 88; brackets show 20 total bytes, 4 bytes per element, and count 5.

Why the same formula fails in a function parameter

Compare the caller's array with this callee:

c
size_t wrong_count(int values[]) {
    return sizeof values / sizeof values[0];
}

int marks[] = {72, 81, 90, 64, 88};

In a function parameter list, int values[] is adjusted to int *values. The callee therefore measures a pointer in the numerator, not the caller's complete array.

On an example system where a pointer occupies eight bytes and an int occupies four bytes, wrong_count calculates 8 / 4 == 2. The caller's array still contains five elements, but that count was not carried into the function. These sizes are one platform example, not portable promises. Compilers commonly warn when sizeof is applied to an array parameter in this pattern.

Diagram contrasts the five-element marks array, 20 bytes total, with the function's 8-byte int pointer, producing 5 versus 2 in the sizeof ratio.

An array converts to a pointer in many expressions. The important exception used here is that sizeof applied directly to the array in its declaring scope measures the full array object. Pointers and Arrays in C: Decay, Indexing and Worked Examples owns the broader decay, indexing, and pointer-to-array treatment; this post isolates the sizeof byte ratio and its function-parameter failure.

Safe function interfaces: pass the count with the pointer

Pass the count explicitly when a function receives a pointer:

c
void print_marks(const int *values, size_t count) {
    for (size_t i = 0; i < count; ++i) {
        printf("%d%s", values[i], i + 1 == count ? "\n" : ", ");
    }
}

int marks[] = {72, 81, 90, 64, 88};
print_marks(marks, sizeof marks / sizeof marks[0]);

The caller calculates count == 5 while marks is still an array. The callee then visits indices 0, 1, 2, 3, and 4, printing 72, 81, 90, 64, 88 in that order.

A macro can make the local calculation shorter:

c
#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))

Use this basic portable macro only with actual arrays. It cannot safely infer that its argument is an array, so passing a pointer recreates the same bug. A struct that packages a pointer and length together is another sound interface. The invariant is simple: the component that declares the array or owns the allocation must preserve and pass its count.

Edge cases that reveal whether the rule is understood

Dynamic allocation. Here, values is a pointer, so sizeof values measures only that pointer. The stored count remains the authoritative length:

c
size_t count = 5;
int *values = malloc(count * sizeof *values);
/* Use values[0] through values[count - 1] after checking allocation. */
free(values);

Partial initialisation. int flags[8] = {1, 1}; still creates eight elements. The formula returns 8, and the remaining six elements are zero-initialised. The number of written initialisers is not the array size.

Character arrays. For char code[] = "C", sizeof code is 2 because the array contains 'C' and the terminating '\0'. In contrast, strlen(code) is 1. Array storage size and string length answer different questions.

How exams and interviews test the trap

Try this prediction drill on a system where sizeof(int) == 4 and sizeof(int *) == 8:

c
int x[10];
int *p = x;

For the array, sizeof x / sizeof x[0] is 40 / 4 == 10. For the pointer, sizeof p / sizeof p[0] is 8 / 4 == 2. Before calculating, identify the operand's type at that exact point.

Use this three-question check:

  1. Is this identifier an array here?

  2. Has it crossed a function boundary?

  3. Are the pointer and element sizes explicitly supplied?

That method is safer than memorising that the formula always works. It works only when its numerator measures a complete array.

Short version and the next practice step

Keep three rules:

  1. Divide the complete array's bytes by one element's bytes.

  2. Perform that division only where the operand is still an array.

  3. Pass a separate size_t count with every pointer-based interface.

The C Language Course: Concepts, MCQs & Coding is the primary next step for reinforcing sizeof, arrays, pointers, and function parameters. For practice that places arrays inside a wider set of structures, continue with the Data Structures course.

Compile the five-mark example, move the formula into wrong_count, enable compiler warnings, and explain in one sentence why the observed result changes.