Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Because in C the array length has to be stated when the array is defined, would it be acceptable practice to use the first element as the length, e.g.

int arr[9]={9,0,1,2,3,4,5,6,7};

Then use a function such as this to process the array:

int printarr(int *ARR) {
    for (int i=1; i<ARR[0]; i++) {
        printf("%d ", ARR[i]);
    }
} 

I can see no problem with this but would prefer to check with experienced C programmers first. I would be the only one using the code.

like image 872
Ian Stewart Avatar asked Jan 28 '21 12:01

Ian Stewart


People also ask

What is the recommended way to establish the length of an array?

Subtracting the address of the start of the array, from the address of the end of the array,​ gives the length of the array.

Is it necessary to declare size of array in C?

You can declare an array without a size specifier for the leftmost dimension in multiples cases: as a global variable with extern class storage (the array is defined elsewhere), as a function parameter: int main(int argc, char *argv[]) .

What is the correct way to write a programming C array?

To create an array, define the data type (like int ) and specify the name of the array followed by square brackets []. To insert values to it, use a comma-separated list, inside curly braces: int myNumbers[] = {25, 50, 75, 100};

Is it necessary to give the size of an array in an array declaration?

We need to give the size of the array because the complier needs to allocate space in the memory which is not possible without knowing the size. Compiler determines the size required for an array with the help of the number of elements of an array and the size of the data type present in the array.


1 Answers

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

like image 185
klutt Avatar answered Oct 16 '22 20:10

klutt