hpique
hpique

Reputation: 120324

Why isn't there an array length function in C?

While there are various ways to find the length of an array in C, the language doesn't provide one.

What was the reason for not including such a common operation in C or any of its revisions?

Upvotes: 3

Views: 5105

Answers (5)

John Bode
John Bode

Reputation: 123448

One of the guiding philosophies of C's design is that all data types map directly to memory, and attempting to store metadata for array types such as length runs counter to that philosophy.

From an article by Dennis Ritchie describing the development of C, we find this:

Embryonic C
...
These semantics represented an easy transition from B, and I experimented with them for some months. Problems became evident when I tried to extend the type notation, especially to add structured (record) types. Structures, it seemed, should map in an intuitive way onto memory in the machine, but in a structure containing an array, there was no good place to stash the pointer containing the base of the array, nor any convenient way to arrange that it be initialized. For example, the directory entries of early Unix systems might be described in C as

    struct {
        int     inumber;
        char    name[14];
    };
I wanted the structure not merely to characterize an abstract object but also to describe a collection of bits that might be read from a directory. Where could the compiler hide the pointer to name that the semantics demanded? Even if structures were thought of more abstractly, and the space for pointers could be hidden somehow, how could I handle the technical problem of properly initializing these pointers when allocating a complicated object, perhaps one that specified structures containing arrays containing structures to arbitrary depth? The solution constituted the crucial jump in the evolutionary chain between typeless BCPL and typed C. It eliminated the materialization of the pointer in storage, and instead caused the creation of the pointer when the array name is mentioned in an expression. The rule, which survives in today's C, is that values of array type are converted, when they appear in expressions, into pointers to the first of the objects making up the array.

Emphasis mine. Just replace the term "pointer" with "metadata" in the passage above, and I think we have the answer to your question.

Upvotes: 8

Konstantin Solomatov
Konstantin Solomatov

Reputation: 10332

Array syntax in C is just a syntactic sugar for pointer arithmetic. If you want to have a real array with length and bounds checking, you can create a struct which contains an array pointer and its length and access it only through functions which check bounds.

Upvotes: 0

Adam Liss
Adam Liss

Reputation: 48290

C is not object-oriented, so it has no concept of methods that are attached to objects. It was designed with speed and simplicity in mind, and the common idiom sizeof (array) / sizeof(array[0]) is short and straightforward.

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 59997

It is down to efficiency. C is a very efficient programing language.

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

Unless someone here happens to be on the C standard committee, you're unlikely to get an authoritative answer. But two reasons I can think of:

  1. In many (most?) situations, you don't have an array, you just have a pointer.

  2. Storing metadata about the array increases the storage size, etc. The general rule of C is that you don't pay for what you don't use.

Upvotes: 4

Related Questions