[comp.lang.c] void declarations and casts

romke@targon.UUCP (Romke Teerenstra) (10/22/87)

This afternoon I had this weird idea to print void values, and to
cast values to void, just to see what would come out.
 My thoughts were : since void is a type, I can declare such a variable.
So I can assign a value of that type to it.
So I can cast something, say an integer, to it.
So I should be ble to write a program what does just that.
To my suprise
however, this didn't even compile on the two types of machines
we have here. One compiler complained about a void type being declared
of which he didn't know the size, and the other just stopped, because
he couldn't cast a value to void.
 After talking it over with two of my collegues,
We think this program is legal, but don't know for sure.
So I ask you:

1   Is this program legal?

2   If not, what is wrong with it?

3   If it is, what should be printed?

#include <sys/types.h>

main()
{
	void v;
	v =
	(void)345;
	printf("%d %c %X\n",(int)v,(char)v,(int)v);

}

As this is cross-posted, please send Follow-ups to comp.lang.c

    Romke

guy%gorodish@Sun.COM (Guy Harris) (10/23/87)

> We think this program is legal, but don't know for sure.
> So I ask you:
> 
> 1   Is this program legal?

No.

> 2   If not, what is wrong with it?

It declares a value of type "void".
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com

cef@H.GP.CS.CMU.EDU (Charles Fineman) (10/23/87)

First to the declaration:
  *Whenever* you try to declare something that the C compiler does not
 know the size of, it complains. For example, you can say:

	struct foo;

  as a forward declaration and then say:

	struct foo *ptr;

  later in your code to get a pointer to a structure of type foo
  but you can't say:

	struct foo var;

  because it dosn't know the size of the thing. Its the same exact
  thing for the void type. Since C dosn't know what size a thing
  of type void should be, you can't declare a variable of that
  type. 

    **  One iteresting side note: there was a discusion a while 
    ** ago on some newsgroup about some folks declaring:
    **     typedef void *generic_pointer;

   As for casting to type void, there is no problem here as long 
 as you realize that the *specified* semantics of this is to
 throw away the value. Hence, casting anything to void makes it
 *not* an rvalue (i.e. something we get a value of).

   Sure, you program is sytactically correct, but then so was 
 that bogus fortran program that sent that poor satalite
 careening off to where no man has gone before ;-)

	Charles Fineman
	Carnegie-Mellon University
	cef@h.cs.cmu.edu (via seismo)

gwyn@brl-smoke.ARPA (Doug Gwyn ) (10/27/87)

In article <359@targon.UUCP> romke@targon.UUCP (Romke Teerenstra) writes:
> My thoughts were : since void is a type, I can declare such a variable.

RONG

>So I can assign a value of that type to it.

RONG

>So I can cast something, say an integer, to it.

TRUE

While there are "void expressions", there are no "void variables".
Once you have "voided" a value, you can't do anything else with it
(except void it again).