Passing sizes of arrays without a separate argument for the length

Michael J. Steiner U23405 at UICVM
Fri Sep 2 10:38:28 AEST 1988


I just got the idea of... well, let me give some examples:

BEFORE - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
main()
{
     char array[10];
     ...
     somefunc(array,10);
}

void somefunc(array,max)
char *array;
int max;
{
     int i;
     for (i = 1; i < max; ++i)
     ...
}


AFTER (MY IDEA) : - - - - - - - - - - - - - - - - - - - - - - - - - - - -
main()
{
     char array[10];
     ...
     array[0] = 10;
     somefunc(array);
}

somefunc(array)
char *array;
{
     int i;
     for (i = 1; i < array[0]; ++i)
     ...
}

In other words, I had an idea of putting the maximum array dimension in
the array itself. This should work with most arrays (since chars, floats,
unsigneds, shorts, etc. can be converted to integers). Also, there is always
room for the size of the array in array[0] (provided that the data starts
at array[1], as in PASCAL), since arrays should have at least a few
elements.

Any comments or suggestions are appreciated.

                                                 Michael Steiner
                                                 Email: U23405 at UICVM.BITNET



More information about the Comp.lang.c mailing list