Void variables

R

Raj Pashwar

Hello

I would like to define a variable of void type. However I get compiler
errors:

void x; // error: variable or field ‘x’ declared void

Infact there is a typedef and I want to allow the defined type to be,
void. However, the error is the same.

Thankyou
 
J

John Gordon

In said:
I would like to define a variable of void type. However I get compiler
errors:
void x; // error: variable or field ‘x’ declared void
Infact there is a typedef and I want to allow the defined type to be,
void. However, the error is the same.

I believe you are only allowed to declare void pointers, not plain voids.

This is allowable:

void *x;

But not this:

void x;
 
I

Ian Collins

Hello

I would like to define a variable of void type. However I get compiler
errors:

void x; // error: variable or field ‘x’ declared void

Infact there is a typedef and I want to allow the defined type to be,
void. However, the error is the same.

void is an incomplete type. You can't do anything with one, so why
would you want one?
 
J

Joe Pfeiffer

Raj Pashwar said:
Hello

I would like to define a variable of void type. However I get compiler
errors:

void x; // error: variable or field ‘x’ declared void

Infact there is a typedef and I want to allow the defined type to be,
void. However, the error is the same.

Thankyou

As others have pointed out, you can't do this. This is one of those
questions that leads me to believe that there's something else you're
actually trying to do, and declaring a variable of type void seems like
a way to achive that underlying goal. So... what are you really trying
to do?
 
K

Kenny McCormack

void is an incomplete type. You can't do anything with one, so why
would you want one?

I wonder if the goal was to get access to what in some languages (*) is
referred to as "WORD" - that is, something that isn't really an int or an
unsigned or anything else - but is just a unit of memory of whatever size
the native word size on the machine is. I think BCPL (a C ancestor) had
this.

(*) Or C with MS's Windows defines in place.
 
J

James Kuyper

Hello

I would like to define a variable of void type. However I get compiler
errors:

void x; // error: variable or field ‘x’ declared void

Infact there is a typedef and I want to allow the defined type to be,
void. However, the error is the same.

You're making essentially the same fundamental conceptual mistake as a
little child who says "I want a nothing. Give me a nothing! Why won't
you give me a nothing?"

Why do you think that you'd like to define a variable of void type? What
would you expect to be able to do with it? We won't be able to show you
how to define an object of void type, there's no such thing. However, if
we know what you wanted to do with it, we could probably suggest
alternative ways of achieving the same goals.
 
8

88888 Dihedral

John Gordonæ–¼ 2012å¹´5月22日星期二UTC+8上åˆ6時05分39秒寫é“:
I believe you are only allowed to declare void pointers, not plain voids.

This is allowable:

void *x;

But not this:

void x;

The type void * is useful for holding an address that supports the polymorphism in the types of the content poined by a pointer in the run time.
 
G

Guest

I would like to define a variable of void type. However I get compiler
errors:

void x; // error: variable or field ‘x’ declared void

Infact there is a typedef and I want to allow the defined type to be,
void. However, the error is the same.

there is no such thing as a void type. What do you think it means. If you explain we might be able to tell you how to achieve what you want some otherway.
 
G

Guest

I wonder if the goal was to get access to what in some languages (*) is
referred to as "WORD" - that is, something that isn't really an int or an
unsigned or anything else - but is just a unit of memory of whatever size
the native word size on the machine is. I think BCPL (a C ancestor) had
this.

(*) Or C with MS's Windows defines in place.



I wonder if the goal was to get access to what in some languages (*) is
referred to as "WORD" - that is, something that isn't really an int or an
unsigned or anything else - but is just a unit of memory of whatever size
the native word size on the machine is. I think BCPL (a C ancestor) had
this.

yes. But that's really *all* it had. int is probably C's nearest approach to a Word.
(*) Or C with MS's Windows defines in place.

not really
 
B

BartC

Kaz Kylheku said:
Furthermore, among what domain values would it vary?

It wouldn't have any values. Or rather, it would only have one. Since it's
value can then always be implied, it wouldn't actually need any memory.

That means an array of 1000000 (or even SIZE_MAX) void values would use zero
bytes.

I can see this being useful as a marker, in a list of struct fields, or in a
parameter list. So If a struct has fields a,b,c,d,e, then printing them
might give:

(1200, 6, void, 5.6, 0)

when c is void. It doesn't look like it adds any useful information, but
it's presence, and the fact it could be any of the five, does provide
something extra. So if b was void instead, then the output might be:

(1200, void, 6, 5.6, 0)

In both cases, the bit-pattern is the same. Of course, it can only vary
between one compilation and the next, not during at runtime, but that
applies also to a lot of conditional macro preprocessor code.

It can also be made slightly more useful, with a directive to map a void
'value' to a specific numeric or string constant:

#pragma void is 42

void x;

printf("X is %d\n",x);

will display "42".
 
E

Eric Sosman

there is no such thing as a void type. What do you think it means. If you explain we might be able to tell you how to achieve what you want some other way.

The void type certainly exists, and is described by 6.2.5p19:

"The void type comprises an empty set of values; it is an
incomplete object type that cannot be completed."

ITYM there is no such thing as a void *object*, hence no such thing
as a void variable, an array of voids, and so on.
 
S

Stephen Sprunk

It wouldn't have any values. Or rather, it would only have one. Since
it's value can then always be implied, it wouldn't actually need any
memory.

That means an array of 1000000 (or even SIZE_MAX) void values would use
zero bytes.

I don't think that's possible since pointers to different objects must
compare unequal. Likewise, offsets of different members of a struct
must be unique. Finally, given that the representation of a (void *)
must be the same as the representation of a (char *), it would seem most
logical for sizeof(void) to be 1, with zero value bits and CHAR_BIT
padding bits.
It can also be made slightly more useful, with a directive to map a void
'value' to a specific numeric or string constant:

#pragma void is 42

void x;

printf("X is %d\n",x);

will display "42".

I have a serious problem with that. AFAIK, every other type must be
able to represent the value 0, and (void) should as well. It's just
that a (void) couldn't represent any _other_ value.

S
 
J

James Kuyper

It wouldn't have any values. Or rather, it would only have one.

You were right the first time. "one value" is one more than the number
specified by the standard: "The void type comprises an empty set of
values" (6.2.5p19).
 
B

BartC

James Kuyper said:
You were right the first time. "one value" is one more than the number
specified by the standard: "The void type comprises an empty set of
values" (6.2.5p19).

An 8-bit type has 256 distinct values. An N-bit one would have 2**N values.

Since a 'void' type would have zero bits, that suggests it would have 2**0
values, or just one.
 
B

Barry Schwarz

An 8-bit type has 256 distinct values. An N-bit one would have 2**N values.

Since a 'void' type would have zero bits, that suggests it would have 2**0
values, or just one.

You are assuming the function is continuous over the number of bits.
The standard clearly states it is not continuous at the value N = 0.
 
J

James Kuyper

James Kuyper said:
On 05/22/2012 05:32 AM, BartC wrote: [Re: an object of void type]
It wouldn't have any values. Or rather, it would only have one.

You were right the first time. "one value" is one more than the number
specified by the standard: "The void type comprises an empty set of
values" (6.2.5p19).

An 8-bit type has 256 distinct values. An N-bit one would have 2**N values.

Since a 'void' type would have zero bits, that suggests it would have 2**0
values, or just one.

The standard does not say that objects of void type require zero bits;
it says that objects cannot have that type. It does say that "The void
type comprises an empty set of values", so inverting your argument, that
would suggest that an object of void type should contain -INFINITY bits.
That presents certain practical problems :), so I suppose that's one
reason why we're not allowed to create such an object.
 
B

BartC

James Kuyper said:
James Kuyper said:
On 05/22/2012 05:32 AM, BartC wrote: [Re: an object of void type]
It wouldn't have any values. Or rather, it would only have one.

You were right the first time. "one value" is one more than the number
specified by the standard: "The void type comprises an empty set of
values" (6.2.5p19).

An 8-bit type has 256 distinct values. An N-bit one would have 2**N
values.

Since a 'void' type would have zero bits, that suggests it would have
2**0
values, or just one.

The standard does not say that objects of void type require zero bits;
it says that objects cannot have that type. It does say that "The void
type comprises an empty set of values", so inverting your argument, that
would suggest that an object of void type should contain -INFINITY bits.
That presents certain practical problems :), so I suppose that's one
reason why we're not allowed to create such an object.

So a void type is just the empty set of values { }.

But the empty set is itself a value. So the empty set must contain itself:
{{{...}}}.

There seems to be some paradox here.
 
J

James Kuyper

So a void type is just the empty set of values { }.

But the empty set is itself a value. So the empty set must contain itself:
{{{...}}}.

There seems to be some paradox here.

No, just a problem with your logic.

You could define a type that includes the empty set among it's
representable values - such a type could be useful for applications
involving set theory. However, the C standard says that the set of
values of void type is empty, so in particular, it cannot include the
empty set itself as a value.

Maybe that argument would be clearer if I used a non-void type for a
comparison. The set of values that comprise the 'int' type is the
integers from INT_MIN to INT_MAX; that set is not, itself, one of the
values contained in that set; all of the members of that set are
integers, none of them are sets of integers.

Your conclusion that "the empty set must contain itself" is wrong; it
wouldn't be "the empty set" if that conclusion were true. There are
people here with a stronger mathematical background than I have; I hope
that one of them can explain more precisely than I can what's wrong with
that conclusion.

You don't explain how you went from your premise to your conclusion.
Even if the premise were correct, I can't come up with any valid
argument connecting it to your conclusion.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top