Information hiding
William E. Davidsen Jr
davidsen at steinmetz.ge.com
Wed Apr 27 00:24:50 AEST 1988
One of the things which bothers me about the C language is that many
(most) compilers, and lint, complain about taking the address on an
array. Not that it isn't redundant, but there is a good reason for
allowing it as an official part of the language: information hiding.
Information hiding is used to improve modularity, in the sense that no
program which is not manipulating the details of a data type need know
about them. This allows the data type to be redefined in a later version
of a package, or defined in a diferent way on another system, and not
require changes to the source code which uses the data type as a "black
box". Ada programmers are astonished to find that C can do information
hiding at all...
If I want to have a type which is defined in a header file and
manipulated by a series of routines, I can do something like:
typedef int mytype[5]; /* this is the user type */
and write routines to work with these types. However, if I need to take
the address of a type, to pass to a procedure, if I say:
mytype a, b, c;
.
init_mt(&a);
I get a warning about taking the address of an array.
Since the whole object of information hiding is to allow things to be
changed between implementations, I don't want to have the user treat
'mytype' as an array, since the next version might use a struct.
Therefore I must use a construct like:
typedef struct {
int dummy[5];
} mytype;
which allows me to take the address freely without telling the user
anything about the actual type.
NOTE: This applies to addresses of procedures, too, in some cases.
--
bill davidsen (wedu at ge-crd.arpa)
{uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me
More information about the Comp.lang.c
mailing list