Article View: comp.lang.modula2
Article #3168Re: Modula2 for C programmers?
From: Peter Moylan
Date: Tue, 23 Jan 1996 04:28
Date: Tue, 23 Jan 1996 04:28
45 lines
2835 bytes
2835 bytes
Eric Obermuhlner (erico@cnmat.berkeley.edu) wrote:
>How do I do the ugly things that C programmers like so very much in Modula 2?
>Like getting a pointer to a var, pointer arithmetic, bit operations (shift, and, or, exor).
All of these things are possible (but see below).
(a) p := ADR(v) makes p point to v;
(b) Pointer arithmetic: support varies from one compiler to another,
but in the libraries you'll likely find procedures like
SYSTEM.AddOffset and the like. Early versions of Modula-2
even allowed arithmetic on variables of type ADDRESS, but
this mistake has since been corrected.
(c) Bit operations: usually supported in some low-level library
module. If not, you can fake them by type-casting to
BITSET, though this is a messy solution.
The obvious question, though, is why you would want to do this.
Such operations are common in C programming because C lacks
more elegant ways to get the job done. In Modula-2, there are
certainly situations where you want to do weird hardware-dependent
things and break the type system, but those situations should
arise only when writing very low-level modules. In most other
cases, there's almost always a better way to do the job.
Why, then, do C programmers like the ugly style so much?
I can think of two main reasons:
1. For whatever reason, C does not support the concept of an
"array". (The syntax makes it look as if arrays are
supported, but when you dig deeper you discover that it's
just a shortcut notation for certain pointer operations.)
Thus C programmers get used to the idea of doing things
like stepping a pointer through an array, even when it
would be easier and clearer - and often more efficient -
to use subscripts.
2. Student programmers are often misled into thinking that
low-level programming leads to greater efficiency.
(Especially when they've graduated from using Basic to
C; there's no doubt that C is faster than Basic, but then
anything's faster than Basic.) Often they're wrong, but
they're so convinced that this conclusion is "intuitively
obvious" that they simply refuse to make the comparison.
--
Peter Moylan peter@ee.newcastle.edu.au
ftp://ee.newcastle.edu.au/pub/www/Moylan.html
Technical Details
Message-ID:
<4e1o5v$6i8@seagoon.newcastle.edu.au>Path:
arc1-ca.pugleaf.net!fourier.newcastle.edu.au!peterReferences:
<erico-1801961940460001@infinitehell.cnmat.berkeley.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fourier.newcastle.edu.au!peter From: peter@fourier.newcastle.edu.au (Peter Moylan) Newsgroups: comp.lang.modula2 Subject: Re: Modula2 for C programmers? Date: 23 Jan 1996 04:28:47 GMT Organization: The University of Newcastle Message-ID: <4e1o5v$6i8@seagoon.newcastle.edu.au> References: <erico-1801961940460001@infinitehell.cnmat.berkeley.edu> Reply-To: peter@tesla.newcastle.edu.au NNTP-Posting-Host: fourier.newcastle.edu.au X-Newsreader: TIN [version 1.2 PL2]