Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!mailrus!tut.cis.ohio-state.edu!bloom-beacon!bu-cs!buengc!bph From: bph@buengc.BU.EDU (Blair P. Houghton) Newsgroups: comp.lang.c Subject: Re: Passing sizes of arrays without a separate argument for the length Message-ID: <1033@buengc.BU.EDU> Date: 20 Sep 88 21:15:28 GMT References: <8809191507.AA17512@ucbvax.Berkeley.EDU> Reply-To: bph@buengc.bu.edu (Blair P. Houghton) Followup-To: comp.lang.c Organization: Boston Univ. Col. of Eng. Lines: 46 In article <8809191507.AA17512@ucbvax.Berkeley.EDU> U23405@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