Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Void variables

Reply
Thread Tools

Void variables

 
 
Bartc
Guest
Posts: n/a
 
      05-23-2012
"David T. Ashley" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...


> Think about it ... why would the C language designers have allowed the
> return type of "void" unless they wanted this to be assignable to a
> variable of type "void"?


Because C uses a 'void' type to identify those functions which are really
procedures.

It further overloads 'void' in a function signature, to specify functions
(or procedures) with 0 parameters, rather than an unknown number.

I agree C could have maybe done more with 'void', after having invented it.

--
Bartc


 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      05-24-2012
somenath wrote:
> On May 23, 10:27 am, Jens Gustedt <(E-Mail Removed)> wrote:
>> Am 23.05.2012 01:14, schrieb Tim Rentsch:
>>
>>> The example program has no structs or unions in
>>> it, so 6.7.2.1 doesn't apply.

>>
>> But 6.7 para 7 applies
>>
>> If an identifier for an object is declared with no linkage, the type
>> for the object shall be
>> complete by the end of its declarator, or by the end of its
>> init-declarator if it has an
>> initializer; in the case of function parameters (including in
>> prototypes), it is the adjusted
>> type (see 6.7.6.3) that is required to be complete.

>
> Out my curiosity when I compiled the following program I did not get
> any diagnostic for the incomplete type. So Will v be treated as
> complete type then ?
>
> int main(void)
> {
> extern void v;


This only declares it but doesn't define nor use it

> return 0;
> }
>
> $ gcc -ansic -pedantic -Wall voidTest.c
> voidTest.c: In function `main':
> voidTest.c:5: warning: unused variable `v'
>
>
> Also please see the following program
> #include<stdio.h>
>
> extern void v;
>
> int main(void)
> {
> int v = 2;


This 'shadows' the prev declarion with a new variable

> printf("Value of v = %d\n",v);
> return 0;
> }
>
> The Out put is
> Value of v = 2
> So is the type of v getting changed dynamically from incomplete to
> complete type ?


No
 
Reply With Quote
 
 
 
 
Joel C. Salomon
Guest
Posts: n/a
 
      05-25-2012
On 05/21/2012 05:53 PM, Raj Pashwar wrote:
> 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.


The problem is that void variables cannot be named; more formally, a
void object can never be an lvalue. (This is why you cannot dereference
a void* pointer.) Unfortunately, the line

void /* no variable name */ ;

is not valid. This was due to a bug in the original C grammar, kept for
backward compatibility reasons. ANSI therefore defined a special syntax

(void) 0;

for the operation you want.

--Joel

P.S. Exercise: Which of my claims above are (a) correct, (b) *almost*
correct but misleadingly phrased or otherwise subtly wrong, or (c)
outright lies?
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      05-25-2012
On 05/21/2012 05:53 PM, Raj Pashwar wrote:
> 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.


It just occurred to me what your last paragraph might mean. Are you
trying to to this:

typedef void x;

If so, you shouldn't be getting the same error message.
--
James Kuyper
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      05-25-2012
On 05/25/2012 08:03 AM, Joel C. Salomon wrote:
> On 05/21/2012 05:53 PM, Raj Pashwar wrote:
>> 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.

>
> The problem is that void variables cannot be named; more formally, a
> void object can never be an lvalue. (This is why you cannot dereference
> a void* pointer.) Unfortunately, the line
>
> void /* no variable name */ ;
>
> is not valid. This was due to a bug in the original C grammar, kept for
> backward compatibility reasons. ANSI therefore defined a special syntax
>
> (void) 0;
>
> for the operation you want.


He didn't say anything about wanting an operation of any kind - most of
the responses have asked him what he actually wants to do
He has said that "I would like to define a variable of void type; what
you've provided is an expression-statement, not a definition of a variable.
--
James Kuyper
 
Reply With Quote
 
Raj Pashwar
Guest
Posts: n/a
 
      05-25-2012
On Tue, 22 May 2012 00:15:32 -0700, nick_keighley_nospam wrote:
> On Monday, May 21, 2012 10:53:26 PM UTC+1, Raj Pashwar wrote:
>
>> 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 other way.


Thanks for the many replies, just got back from a trip. Sorry I don't
have time to read them all fully, this thread has got too long.

The situation is this: I have a typedef'd variable. This might be
typedef'd to say FILE*:

typedef FILE* mytype;

and then the code
mytype x;

compiles fine.

However, if instead it is typedef'd to void:

typedef void mytype;

then the same code
mytype x;

no longer compiles.

I hope this is clearer.

Cheers Raj
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-25-2012
On 5/25/2012 5:22 PM, Raj Pashwar wrote:
> On Tue, 22 May 2012 00:15:32 -0700, nick_keighley_nospam wrote:
>> On Monday, May 21, 2012 10:53:26 PM UTC+1, Raj Pashwar wrote:
>>
>>> 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 other way.

>
> Thanks for the many replies, just got back from a trip. Sorry I don't
> have time to read them all fully, this thread has got too long.


Oh, I see. *We* can spend *our* time writing answers to your
question, but *you* can't be bothered to read them? Sweet ...

> The situation is this: I have a typedef'd variable. This might be
> typedef'd to say FILE*:
>
> typedef FILE* mytype;
>
> and then the code
> mytype x;
>
> compiles fine.
>
> However, if instead it is typedef'd to void:
>
> typedef void mytype;
>
> then the same code
> mytype x;
>
> no longer compiles.
>
> I hope this is clearer.


Several respondents have explained why it won't compile.

One thing remains UNclear: What are you trying to achieve?
The meaningless `void x;' (in whatever spelling) won't help, so
simply repeating that it doesn't gets us nowhere. Tell us what
you want to accomplish, and someone will quite likely suggest an
approach that will get you nearer your goal.

Assuming you take the trouble to read the suggestion, that is.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      05-25-2012
On 05/25/2012 05:22 PM, Raj Pashwar wrote:
....
> The situation is this: I have a typedef'd variable. This might be
> typedef'd to say FILE*:
>
> typedef FILE* mytype;
>
> and then the code
> mytype x;
>
> compiles fine.
>
> However, if instead it is typedef'd to void:
>
> typedef void mytype;
>
> then the same code
> mytype x;
>
> no longer compiles.
>
> I hope this is clearer.


It is.

The simple answer is, you can't do that, and you should re-write your
code to avoid doing it. If it's not obvious how the re-write should be
done, we can give you advice on that matter. But in order to give you
that advice, we would need more information about the context than
you've given us:

Under what circumstances would 'mytype' be void?
What is the purpose of 'x'?

It should be the case that the purpose served by 'x' becomes meaningless
under those circumstances where 'mytype' is void. If not, you've got big
problems; otherwise, something similar to the following example might do
the trick:

#define VOID_MYTYPE

typedef
#ifdef VOID_MYTYPE
void
#else
other_type
#endif
mytype;

mytype *func(
mytype *p
#ifndef VOID_MYTYPE
, mytype x
#endif
){
#ifndef VOID_MYTYPE
*p++ = x;
#endif
// Other operations not involving x
return p;
}

You have to use the #if #else #endif family for this purpose, you can't
use if(){}else{}, because the definitions of 'x' and 'y' are constraint
violations if mytype is void. You must make sure that they don't even
exist anymore after preprocessing is complete, if that is the case.
 
Reply With Quote
 
Joe Pfeiffer
Guest
Posts: n/a
 
      05-26-2012
Raj Pashwar <(E-Mail Removed)> writes:

> On Tue, 22 May 2012 00:15:32 -0700, nick_keighley_nospam wrote:
>> On Monday, May 21, 2012 10:53:26 PM UTC+1, Raj Pashwar wrote:
>>
>>> 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 other way.

>
> Thanks for the many replies, just got back from a trip. Sorry I don't
> have time to read them all fully, this thread has got too long.
>
> The situation is this: I have a typedef'd variable. This might be
> typedef'd to say FILE*:
>
> typedef FILE* mytype;
>
> and then the code
> mytype x;
>
> compiles fine.
>
> However, if instead it is typedef'd to void:
>
> typedef void mytype;
>
> then the same code
> mytype x;
>
> no longer compiles.
>
> I hope this is clearer.


No, it was perfectly clear the first time. And many people answered the
first time. Why would anyone answer again?
 
Reply With Quote
 
Raj Pashwar
Guest
Posts: n/a
 
      05-26-2012
On Fri, 25 May 2012 17:38:34 -0400, Eric Sosman wrote:

> On 5/25/2012 5:22 PM, Raj Pashwar wrote:
>> On Tue, 22 May 2012 00:15:32 -0700, nick_keighley_nospam wrote:
>>> On Monday, May 21, 2012 10:53:26 PM UTC+1, Raj Pashwar wrote:
>>>
>>>> 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 other way.

>>
>> Thanks for the many replies, just got back from a trip. Sorry I don't
>> have time to read them all fully, this thread has got too long.

>
> Oh, I see. *We* can spend *our* time writing answers to your
> question, but *you* can't be bothered to read them? Sweet ...
>
>> The situation is this: I have a typedef'd variable. This might be
>> typedef'd to say FILE*:
>>
>> typedef FILE* mytype;
>>
>> and then the code
>> mytype x;
>>
>> compiles fine.
>>
>> However, if instead it is typedef'd to void:
>>
>> typedef void mytype;
>>
>> then the same code
>> mytype x;
>>
>> no longer compiles.
>>
>> I hope this is clearer.

>
> Several respondents have explained why it won't compile.
>
> One thing remains UNclear: What are you trying to achieve?
> The meaningless `void x;' (in whatever spelling) won't help, so simply
> repeating that it doesn't gets us nowhere. Tell us what you want to
> accomplish, and someone will quite likely suggest an approach that will
> get you nearer your goal.
>
> Assuming you take the trouble to read the suggestion, that is.


OK, I will explain, but I didn't think these details are relevant.

The program has a LOG functionality. This may be enabled, then log_t will
be FILE* pointing to the LOG location. Or it may be unenabled when log_t
must be void typed.

It appears a workaround may be needed here.

Cheers Raj
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between void proba(); and void proba(void); ??? PencoOdStip@gmail.com C++ 1 05-23-2007 07:12 PM
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
returning a void (*)(void) Sergio C++ 6 01-05-2005 08:30 PM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM



Advertisments