Passing sizes of arrays without a separate argument for the length

Blair P. Houghton bph at buengc.BU.EDU
Wed Sep 21 07:15:28 AEST 1988


In article <8809191507.AA17512 at ucbvax.Berkeley.EDU> U23405 at UICVM (Michael J. Steiner) writes:
>BEFORE - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>main()
>{
>     char array[10];
>     ...
>     somefunc(array,10);
>}
[...you get the point...]
>
>AFTER (MY IDEA) : - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>main()
>{
>     char array[10];
>     ...
>     array[0] = 10;
>     somefunc(array);
>}
>
>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.

Works fine if you're prepared to keep track of all those casts.  You also
need to remember always that a "char array[10]" is {array[0],...,array[9]}.

Essentially, since you are "creating" the extra data element array[0]
expressly to pass the array size, it seems that you gain little by this
trick.  There's no memory-usage difference inherent in the two schemes.

Actually, it looks like a Great Obfuscation; a logical extension
would be to create "char array[MAX]" and cast _every_ member to something
other than char.  You could effectively give every variable in the program
the same name and a number by which it is truly identified.

It might have uses as a sort of nebulous database, but then that's
what ASCII files are for...

You're better off with "somefunc(array,MAX)"

				--Blair
				  "...but then that's what ASCII
				   files are for..."
				   -famous last words



More information about the Comp.lang.c mailing list