Function Pointer Arrays

What Is an Array in C? Explained With an Example

Learn what an array in C is, how to declare one, how zero-based indexing works, and how to loop over the elements, with a short complete example.

On this page
  1. What is an array in C?
  2. Declaring an array
  3. A complete example
  4. Getting the number of elements
  5. Indexing and bounds
  6. Arrays can hold function pointers too

What is an array in C?

An array in C is a fixed-size collection of values that share one type. The values sit next to each other in memory, and you reach each one with an index. Indexes start at 0, so the first element is at index 0 and the last is at index one less than the size.

Arrays in C and C++ share the same basic syntax, so the example below is written in C++ with std::cout. In C, you would print with printf instead.

Declaring an array

To declare an array, write the element type, a name, and the number of elements in square brackets:

int scores[5];

This reserves room for five int values. A local array with no initializer holds indeterminate values until you assign them, so reading them first is a bug.

You can set the values when you declare the array:

int scores[5] = {90, 85, 77, 92, 88};

If you give fewer values than elements, the rest are set to zero. For example, int ages[4] = {20, 30}; sets the last two elements to 0.

A complete example

#include <iostream>

int main() {
    int scores[5] = {90, 85, 77, 92, 88};

    std::cout << "First score: " << scores[0] << '\n';
    std::cout << "Third score: " << scores[2] << '\n';

    int total = 0;
    for (int i = 0; i < 5; ++i) {
        total += scores[i];
    }

    std::cout << "Total: " << total << '\n';
}

Output:

First score: 90
Third score: 77
Total: 432

The loop uses i as the index, running from 0 to 4. Each pass adds one element to total.

Getting the number of elements

Typing 5 in the loop works, but that number has to change whenever the array size changes. In C++17 and later, std::size(scores) returns the element count. Without it, sizeof can do the job:

const auto count = sizeof(scores) / sizeof(scores[0]);

The sizeof trick only works on the array itself. If the array is passed to a function, the parameter is a pointer, and sizeof gives the size of the pointer instead.

Indexing and bounds

Indexing uses square brackets, so scores[2] is the third element, with the value 77. C arrays do not check the index. Reading scores[5] in the example above goes past the end of the array. That is undefined behavior, so the program might print a stray number, crash, or appear to work.

Because C arrays have a fixed size and no bounds checks, many C++ programs use std::array when the size is fixed and std::vector when it changes. Passing an array into a function has its own rules, which are covered in how to pass an array to a function in c.

Arrays can hold function pointers too

Element types are not limited to numbers. An element can be a pointer to a function, as long as every function in the array has the same signature. That idea leads to c array of function pointers, a table of operations that you can index by position.

The complete guide to function pointer arrays

Arrays of Function Pointers in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

Is C++ Harder Than Python?

For most beginners, C++ is harder than Python because it needs a compiler, explicit types and careful memory handling. See why, with a side-by-side example.

C++ vs Python 4 min read