M
mark
Hi
What is the principle reason why I cant define a void variable, eg
void x; // gives error
What is the principle reason why I cant define a void variable, eg
void x; // gives error
mark said:What is the principle reason why I cant define a void variable, eg
void x; // gives error
mark said:Hi
What is the principle reason why I cant define a void variable, eg
void x; // gives error
mark said:Hi
What is the principle reason why I cant define a void variable, eg
void x; // gives error
Keith said:Why should you be able to? What would you expect to be able to do
with it?
mark said:What is the principle reason why I cant define a void variable, eg
void x; // gives error
Mark McIntyre said:I should imagine he wants to create a typeless variable, and he
expects to later on be able to munge it into a specific type via some
mechanism - perhaps by runtime type conversion when first used. I
believe some languages with a V in their name and possibly ending in
script support this.
To the OP: you can't do it because it doesn't have any meaning. The C
standard defines an expression of type void as having a nonexistent
value (not a value of zero, but no value) and states that this value
is unusable.
Don't be confused by void*, this is a special notation to allow
conversion between pointers of different non-void types.
pete said:void isn't an *object type*.
Keith Thompson said:Why should you be able to? What would you expect to be able to do
with it?
Phil Carmody said:Take the address of it, and know that it was both a valid address,
and not equal to any other address in the system, so that it could
be compared and contrasted using == and != against both addresses
of tangible objects and also other such addresses.
Hi
It should be what you get when dereferencering a void*, eg a generic
variable of any type (like Variant in Visual Basic).
Phil Carmody said:Please don't top-post, it's wrong-headed. Correcting.
You cannot dereference a void*. Therefore you shouldn't get a void,
as you don't get anything when you don't do anything. And a void,
not that it exists as an object type, is nothing like a Variant in
VB, which is more like a union of many types.
mark said:Hi
In that case C is missing a trick IMO, the Variant datatype is
exceptionally powerful in VB and all thats needed is a simple extension of
syntax.
mark said:Hi
In that case C is missing a trick IMO, the Variant datatype is
exceptionally powerful in VB and all thats needed is a simple extension of
syntax.
mark wrote:
Oh no - a lot more than that is needed. Languages that provide such a
feature need a lot of additional machinery in order to make it work;
some of it syntactic, but a lot of it is semantic.. The easiest way to
demonstrate this would be to have you give us an example of code that
you would like to be able to write, making use of a void data type in
C. Then we can tell you just what would have to be added to the C
language and to the typical C implementation in order ot make it work.
I'm not saying it can't be done. Just that doing so would require
major changes both to the C standard and to typical C implementations,
changes that in many cases would make it unacceptably inefficient for
many of the uses it is currently popular for.
mark wrote:
But you *can* do that in C. Just read the include files where that
VARIANT is defined. In oaidl.h we find:
struct _VARIANT {
union {
struct __tagVARIANT {
VARTYPE vt;
WORD wReserved1;
WORD wReserved2;
WORD wReserved3;
union {
LONG lVal; .....
This is a structure that contains a union of all types that Microsoft
wanted to use in a variant. You can design similar structures in C:
struct myVariant {
union {
// put all the types that you want here
};
Bart said:That big struct was designed to implement /another/ language, where
all the untidy implementations details are hidden away.
Trying to use it directly in C code is not going to be pretty, without
some extra support.
Especially when you also want to add your user-defined structs to the
list of variant types.
Nonsense, specially when you use macros to access the member you want.
Basically, this structure lays out clearly what is all about, like
alwys in C the language is transparent.- Hide quoted text -
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.