Is it good programming practice in C to use first array element as array length? Is it good programming practice in C to use first array element as array length? arrays arrays

Is it good programming practice in C to use first array element as array length?


Well, it's bad in the sense that you have an array where the elements does not mean the same thing. Storing metadata with the data is not a good thing. Just to extrapolate your idea a little bit. We could use the first element to denote the element size and then the second for the length. Try writing a function utilizing both ;)

It's also worth noting that with this method, you will have problems if the array is bigger than the maximum value an element can hold, which for char arrays is a very significant limitation. Sure, you can solve it by using the two first elements. And you can also use casts if you have floating point arrays. But I can guarantee you that you will run into hard traced bugs due to this. Among other things, endianness could cause a lot of issues.

And it would certainly confuse virtually every seasoned C programmer. This is not really a logical argument against the idea as such, but rather a pragmatic one. Even if this was a good idea (which it is not) you would have to have a long conversation with EVERY programmer who will have anything to do with your code.

A reasonable way of achieving the same thing is using a struct.

struct container {    int *arr;    size_t size;};int arr[10];struct container c = { .arr = arr, .size = sizeof arr/sizeof *arr };

But in any situation where I would use something like above, I would probably NOT use arrays. I would use dynamic allocation instead:

const size_t size = 10;int *arr = malloc(sizeof *arr * size);if(!arr) { /* Error handling */ }struct container c = { .arr = arr, .size = size };

However, do be aware that if you init it this way with a pointer instead of an array, you're in for "interesting" results.

You can also use flexible arrays, as Andreas wrote in his answer


In C you can use flexible array members. That is you can write

struct intarray {   size_t count;   int data[];  // flexible array member needs to be last};

You allocate with

size_t count = 100;struct intarray *arr = malloc( sizeof(struct intarray) + sizeof(int)*count );arr->count = count;

That can be done for all types of data.

It makes the use of C-arrays a bit safer (not as safe as the C++ containers, but safer than plain C arrays).

Unforntunately, C++ does not support this idiom in the standard.Many C++ compilers provide it as extension though, but it is not guarantueed.

On the other hand this C FLA idiom may be more explicit and perhaps more efficient than C++ containers as it does not use an extra indirection and/or need two allocations (think of new vector<int>).

If you stick to C, I think this is a very explicit and readable way of handling variable length arrays with an integrated size.

The only drawback is that the C++ guys do not like it and prefer C++ containers.


It is not bad (I mean it will not invoke undefined behavior or cause other portability issues) when the elements of array are integers, but instead of writing magic number 9 directly you should have it calculate the length of array to avoid typo.

#include <stdio.h>int main(void) {    int arr[9]={sizeof(arr)/sizeof(*arr),0,1,2,3,4,5,6,7};        for (int i=1; i<arr[0]; i++) {        printf("%d ", arr[i]);    }    return 0;}