[net.lang.c] Why 'void *' is useful

kpmartin@watmath.UUCP (Kevin Martin) (07/09/84)

Pete Delaney asks...
>Why do we need void *, the 'universital pointer', the syntax is questionable?

I think that the word was 'universal'. Perhaps 'generic' might have been
a better word.
Basically, it is a reasonable thing to do with a construct which would
otherwise have to be made illegal.
The idea is to have a pointer which cannot be dereferenced without casting
it first. I find the syntax quite clear (In fact, this meaning for 'void *'
has already been implemented in our C compilers... it was suggested after
the compiler was found to be buggy in its treatment of casts into strange
types). It is a pointer that, if an attempt is made to indirect through it,
yields the illegal/unusable type 'void'.

A non-sensical use of 'void' would be to declare an object of type void
(not function-returning-void). e.g.
f() {
	void x;	/* Now what do I do with it? */
	...
}

chris@umcp-cs.UUCP (07/10/84)

One could always use ``caddr_t'', which is what the Berkeley <sys/types.h>
defines for ``core address type''.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci (301) 454-7690
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@maryland

ado@elsie.UUCP (07/11/84)

>	A non-sensical use of 'void' would be to declare an object of type void
>	(not function-returning-void). e.g.
>	f() {
>		void x;	/* Now what do I do with it? */
>		...
>	}

What do you do with it?  Take "sizeof x", of course. :-)

A possibly sane use would be as a place-holder for "obsolete"
elements of structures stored in files.  Suppose you have a bunch of
files, each of which contains a structure like:

	typedef struct {
		int	fortran;
		int	pascal;
		int	c;
	} languages;

Now suppose you want to stop using "fortran" and "pascal", and yet want to be
able to retrive the information in the "c" element of previously-stored
structures.  You might want to:

	typedef struct {
		void int	fortran;
		void int	pascal;
		int		c;
	} languages;

to enforce discontinuation of use of the "obsolete" elements while preserving
the ability to get at the "c" elements in the files.  Currently, you can
do this by declaring unnamed bit fields; doing this is machine dependent, given
the varying number of bits per int (or whatever) on different machines.

Of course, it STILL doesn't make sense in the above example for "void" to stand
alone--here, it's being used as an adjective.
--
	...decvax!allegra!umcp-cs!elsie!ado	(301) 496-5688
	(the DEC and VAX in decvax are Digital Equipment Corporation trademarks)

henry@utzoo.UUCP (Henry Spencer) (07/13/84)

The trouble with using "caddr_t" to do the "void *" job is that "caddr_t"
is just a human convenience.  Depending on how you do it, "caddr_t" is
either a #defined symbol or a typedef; neither is any more than an
abbreviation for some other type.  Strict type checking will expand the
abbreviation and then complain about the expansion.

I suppose one could make "caddr_t" a magic keyword, but using "void *"
is simpler and doesn't introduce a new keyword.
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry