3 ms·
Back in the 80's, I learned Pascal, and learned about its dynamically allocated records, then I went on to learn C, and got used to its pointers and arrays. Th
by DonHopkins 6y ago
Back in the 80's, I learned Pascal, and learned about its dynamically allocated records, then I went on to learn C, and got used to its pointers and arrays.
Then I went back to Pascal, and designed a program in my head with some dynamically allocated linked list data structures, and another data structure that had a member that pointed to the head of the linked list.
Then I started typing in the Pascal code, and hit a wall, because Pascal has ^ which is like C's * operator to dereference a pointer, but doesn't have anything like C's & operator to make a pointer to an arbitrary field in memory, so you can't actually make a pointer to anything except the beginning of a record that you dynamically allocated!
That was when I gave up on Pascal.
Programming Pascal is like riding a bicycle with only one leg.
- augustk 6y agoMost programming languages don't have an "address of" operator. That's because it's a potentially unsafe operation. I think your usage of Pascal sounds more like trying to operate a motorcycle like a bicycle.
- DonHopkins 6y agoPascal has what it call pointers, but doesn't have what C calls pointer arithmetic, or a way to take the address of any field of a structure or any element of an array. So Linus's elegant linked list solution is possible in C, but not Pascal. I don't get your metaphor that Pascal is a motorcycle and C is a bicycle. Just the opposite.
- benibela 6y agoThat must have been a very old Pascal Nowadays there is the @ operator
- DonHopkins 6y agoYes, very old Pascal, not even TurboPascal! I started with Apple ][ UCSD Pascal, but that program was for HP3000 Pascal, which might be even older. There was a trick for doing PEEK and POKE in Apple Pascal, using a union that contained a pointer to some type you could dereference to read and write, and also an integer so you could set the pointer. I had the hardest time understanding it, and just could not get my head around the "record case boolean of", but I just typed in the magic code with a bunch of weird ^'s and it worked somehow. You can use type punning tricks with "union" to implement arbitrary unsafe pointer arithmetic in Pascal, since it lets you convert between pointers and integers. https://en.wikipedia.org/wiki/Type_punning#Pascal https://en.wikipedia.org/wiki/Type_punning#Pascal Here's an article about that trick, in German (which makes it sound even cooler): https://www.robert-tolksdorf.de/book/Tolksdorf-UCSD-Pascal-CC-BY-NC-ND-4.0.pdf https://www.robert-tolksdorf.de/book/Tolksdorf-UCSD-Pascal-C... p. 63: 3.1 "PEEK" und "POKE" auch in Pascal [...] type byte = 0..255 spchrinhalt = packed array [0..0] of byte; spchrstelle = record case boolean of true: (adresse:integer); ( Zieger ) false: (inhalt:^spchrinhalt) ( Inhalt ) end; procedure poke(adresse:integer; inhalt:byte); var dummy:spchrstelle; begin dummy.adresse := adresse; dummy.inhalt^[0] := inhalt; end; function peek(adresse:integer): byte; var dummy:spchrstelle; begin dummy.adresse := adresse; peek := dummy.inhalt^[0]; end;