jbs@mit-eddie.UUCP (Jeff Siegal) (09/26/85)
>In Pascal the length of an array is part of it's type. Strings are >generally stored in 'packed array of char' (or array of char, no >difference for this point.) Now, consider the strlen function in >Pascal: > >constant > maxstring = 256 ; >type > string: packed array [1..maxstring] of char ; >var > buf : string ; > > function strlen(str : string) : integer ; > begin > strlen = maxstring > end ; { strlen } > >Kinda stupid, huh!? But you just can't pass different length arrays >to a Pascal routine, pointers don't help (as a C programmer might have >guessed) as they are typed according to the object they point to and >you just end up back to where you started. There are no casts or some >such to help. > Not exactly. The ISO Pascal standard allows for "conformant parameters in which a procedure (or function) can be declared as such: function strlen(str:packed array[1..n] of char):integer; begin strlen := n; end; writeln(strlen("Hello There"):1); Perhaps your compiler doesn't supprot this... But that's a different story :-) > 2. You could use some other representation of string, when I > brought this up with a faculty member her solution was > 'you should just represent strings as linked lists of chars' > [no comment] > A better (i.e. usable :-)) solution is to represent strings a linked lists of character "chunks" perhaps 16 characters. This allows both dynamic length strings, and some measure of efficiency. I would perfer though to combine this meathod with another: type blocks: record chars: packed array[1..blocksize] of char; rest: ^blocks end; string: record len:0..maxint; body:^blocks end; > 3. Some Pascals have strings built in as a 'natural' data type: > No, all Pascals do this: "Hello" is defined to be a packed array[1..5] of char. But I suppose this doesn't help the problem of dynamic strings. > > b) More importantly, this is just an example, solving the > special case of strings still doesn't let me write > C's qsort() routine (try it, it *cannot* be written in > Pascal in any useful way...sad, huh, ya gotta write a > different sort routine for every data type, array size > you use!) > see "conformant arrays" above. >The result: Everything ends up to be ad hoc one-shots, forget building >generic libraries, write your own everything. And completely forget >portability, it doesn't exist in the pascal world. > Jeff Siegal - MIT EECS