[A89] Re: Pointers and Matricies


[Prev][Next][Index][Thread]

[A89] Re: Pointers and Matricies




On Fri, Feb 23, 2001, Sebastian Reichelt wrote:
> 
> | What makes Pascal more logical then C?
> 
> The syntax.
>
> | And logical in what way?
> 
> For example, I completely hate the keyword 'void', because it can have many
> meanings and looks just like a normal type.

The keyword 'var' (in Pascal) also has "many" meanings.

'void' looks just like a normal type -- because it IS a normal type.
A `short' usually occupies 2 bytes, a `char' usually occupies 1 byte and
a `void' occupies 0 bytes.  The other meaning of `void' is the empty
argument list.


> Next, I think the way functions
> are declared is pretty strange: They start out like a variable, and then
> they just take parameters.

Yes!  Everything is declared the same way and declarations are consistent
with each other!  A declaration with a type "X" and a declaration with a
type that is derived from "X" (eg. "pointer to X") look very similar -- and
why not, they both contain mostly "X"!!!  This "derived type is similar"
rule applies to ALL types "X", including function declarations.

Also, a type declaration in C follows the actual usage of the variable/
function.  The type declaration `int <something>;' declares <something> to
be an `int'.  By applying this rule blindly, `int *a;' tells us that `*a' is
an `int' -- which is absolutely correct!  This "inverse" interpretation
works with every data type (oh consistency!), including function
declarations.  `int foo(void) { ... }' declares a function, but also, just
like above, correctly tells us that the type of `foo()' is `int'.


> Function types are the worst; what's up with
> "void(*)(void)" (as opposed to a simple "procedure")?

Huh?  "void (*)(void)" is a "pointer to a function" type, not a "function"
type...

Anyway, the reason to use `void foo(void)' instead of `Procedure foo'
follows directly from both the "derived type is similar" "rule" and the
"inverse" interpretation as described above.  When we dereference a "pointer
to function" we must get a "function".  So if we want `*foo' (the de-
referenced pointer `foo') to be of the type "function taking no args
returning int" and we know that the declaration `int <something>(void);'
results in <something> having this type, we know that we can write
`int (*foo)(void);' to get exactly what we want.


> And matrices are
> pretty bad as well:  First there is the type, then the variable name, then
> the dimensions, which belong to the type from my point of view.

Once again I refer to the "inverse" interpretation of declarations.  If
`foo[i]' is an `int' (and it's certain that `foo' is not a pointer etc),
then the declaration of `foo' must be, eg., `int foo[5];'.


> In using
> pointers, I find that the location of the asterisks is 'switched around';
> this also makes more parentheses necessary.

True.  The language is not perfect, but IMHO it's far better than,
say, Pascal.  See also  http://www.lysator.liu.se/c/bwk-on-pascal.html


-- 
Johan Eilert




References: