Void variables

Discussion in 'C Programming' started by Raj Pashwar, May 21, 2012.

  1. Raj Pashwar

    Raj Pashwar Guest

    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
     
    Raj Pashwar, May 21, 2012
    #1
    1. Advertising

  2. Raj Pashwar

    John Gordon Guest

    In <jpedgm$br1$> Raj Pashwar <> writes:

    > 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.


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

    This is allowable:

    void *x;

    But not this:

    void x;

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, May 21, 2012
    #2
    1. Advertising

  3. Raj Pashwar

    Ike Naar Guest

    On 2012-05-21, Raj Pashwar <> wrote:
    > I would like to define a variable of void type.


    How would such a variable be useful?
     
    Ike Naar, May 21, 2012
    #3
  4. Raj Pashwar

    Ian Collins Guest

    On 05/22/12 09:53 AM, 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.


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

    --
    Ian Collins
     
    Ian Collins, May 21, 2012
    #4
  5. Raj Pashwar

    Joe Pfeiffer Guest

    Raj Pashwar <> writes:

    > 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?
     
    Joe Pfeiffer, May 21, 2012
    #5
  6. In article <>,
    Ian Collins <> wrote:
    >On 05/22/12 09:53 AM, 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.

    >
    >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.

    --
    Just for a change of pace, this sig is *not* an obscure reference to
    comp.lang.c...
     
    Kenny McCormack, May 21, 2012
    #6
  7. Raj Pashwar

    James Kuyper Guest

    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.


    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.
     
    James Kuyper, May 21, 2012
    #7
  8. Raj Pashwar

    Kaz Kylheku Guest

    On 2012-05-21, Ike Naar <> wrote:
    > On 2012-05-21, Raj Pashwar <> wrote:
    >> I would like to define a variable of void type.

    >
    > How would such a variable be useful?


    Furthermore, among what domain values would it vary?

    --
    If you ever need any coding done, I'm your goto man!
     
    Kaz Kylheku, May 22, 2012
    #8
  9. John Gordonæ–¼ 2012å¹´5月22日星期二UTC+8上åˆ6時05分39秒寫é“:
    > In <jpedgm$br1$> Raj Pashwar <..com> writes:
    >
    > > 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.

    >
    > I believe you are only allowed to declare void pointers, not plain voids.
    >
    > This is allowable:
    >
    > void *x;
    >
    > But not this:
    >
    > void x;
    >
    > --
    > John Gordon A is for Amy, who fell down the stairs
    > B is for Basil, assaulted by bears
    > -- Edward Gorey, "The Gashlycrumb Tinies"


    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.
     
    88888 Dihedral, May 22, 2012
    #9
  10. Raj Pashwar

    Guest

    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 otherway.
     
    , May 22, 2012
    #10
  11. Raj Pashwar

    Guest

    On Monday, May 21, 2012 11:22:30 PM UTC+1, Kenny McCormack wrote:
    > In article <>,
    > Ian Collins <> wrote:
    > >On 05/22/12 09:53 AM, 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.

    > >
    > >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.
    >
    > --
    > Just for a change of pace, this sig is *not* an obscure reference to
    > comp.lang.c...




    On Monday, May 21, 2012 11:22:30 PM UTC+1, Kenny McCormack wrote:
    > In article <>,
    > Ian Collins <> wrote:
    > >On 05/22/12 09:53 AM, 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.

    > >
    > >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.


    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
     
    , May 22, 2012
    #11
  12. Raj Pashwar

    BartC Guest

    "Kaz Kylheku" <> wrote in message
    news:...
    > On 2012-05-21, Ike Naar <> wrote:
    >> On 2012-05-21, Raj Pashwar <> wrote:
    >>> I would like to define a variable of void type.

    >>
    >> How would such a variable be useful?

    >
    > 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".

    --
    Bartc
     
    BartC, May 22, 2012
    #12
  13. Raj Pashwar

    Eric Sosman Guest

    On 5/22/2012 3:15 AM, 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.


    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.

    --
    Eric Sosman
    d
     
    Eric Sosman, May 22, 2012
    #13
  14. On 22-May-12 04:32, BartC wrote:
    > "Kaz Kylheku" <> wrote in message
    > news:...
    >> On 2012-05-21, Ike Naar <> wrote:
    >>> On 2012-05-21, Raj Pashwar <> wrote:
    >>>> I would like to define a variable of void type.
    >>>
    >>> How would such a variable be useful?

    >>
    >> 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 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

    --
    Stephen Sprunk "God does not play dice." --Albert Einstein
    CCIE #3723 "God is an inveterate gambler, and He throws the
    K5SSS dice at every possible opportunity." --Stephen Hawking
     
    Stephen Sprunk, May 22, 2012
    #14
  15. Raj Pashwar

    James Kuyper Guest

    On 05/22/2012 05:32 AM, BartC wrote:
    > "Kaz Kylheku" <> wrote in message
    > news:...
    >> On 2012-05-21, Ike Naar <> wrote:
    >>> On 2012-05-21, Raj Pashwar <> wrote:
    >>>> I would like to define a variable of void type.
    >>>
    >>> How would such a variable be useful?

    >>
    >> Furthermore, among what domain values would it vary?

    >
    > 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).
     
    James Kuyper, May 22, 2012
    #15
  16. Raj Pashwar

    BartC Guest

    "James Kuyper" <> wrote in message
    news:...
    > On 05/22/2012 05:32 AM, BartC wrote:
    >> "Kaz Kylheku" <> wrote in message
    >> news:...
    >>> On 2012-05-21, Ike Naar <> wrote:
    >>>> On 2012-05-21, Raj Pashwar <> wrote:
    >>>>> I would like to define a variable of void type.
    >>>>
    >>>> How would such a variable be useful?
    >>>
    >>> Furthermore, among what domain values would it vary?

    >>
    >> 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.

    --
    Bartc
     
    BartC, May 22, 2012
    #16
  17. On Tue, 22 May 2012 17:08:42 +0100, "BartC" <> wrote:

    >
    >
    >"James Kuyper" <> wrote in message
    >news:...
    >> On 05/22/2012 05:32 AM, BartC wrote:
    >>> "Kaz Kylheku" <> wrote in message
    >>> news:...
    >>>> On 2012-05-21, Ike Naar <> wrote:
    >>>>> On 2012-05-21, Raj Pashwar <> wrote:
    >>>>>> I would like to define a variable of void type.
    >>>>>
    >>>>> How would such a variable be useful?
    >>>>
    >>>> Furthermore, among what domain values would it vary?
    >>>
    >>> 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.


    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.

    --
    Remove del for email
     
    Barry Schwarz, May 22, 2012
    #17
  18. Raj Pashwar

    James Kuyper Guest

    On 05/22/2012 12:08 PM, BartC wrote:
    >
    >
    > "James Kuyper" <> wrote in message
    > news:...
    >> 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.
     
    James Kuyper, May 22, 2012
    #18
  19. Raj Pashwar

    BartC Guest

    "James Kuyper" <> wrote in message
    news:...
    > On 05/22/2012 12:08 PM, BartC wrote:
    >>
    >>
    >> "James Kuyper" <> wrote in message
    >> news:...
    >>> 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.

    --
    Bartc
     
    BartC, May 22, 2012
    #19
  20. Raj Pashwar

    James Kuyper Guest

    On 05/22/2012 12:30 PM, BartC wrote:
    >
    >
    > "James Kuyper" <> wrote in message
    > news:...

    ....
    >> 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.


    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.
     
    James Kuyper, May 22, 2012
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sergio

    returning a void (*)(void)

    Sergio, Jan 5, 2005, in forum: C++
    Replies:
    6
    Views:
    442
    Jonathan Turkanis
    Jan 5, 2005
  2. Ollej Reemt
    Replies:
    7
    Views:
    567
    Jack Klein
    Apr 22, 2005
  3. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    812
    The Real OS/2 Guy
    Oct 28, 2003
  4. Replies:
    5
    Views:
    850
    S.Tobias
    Jul 22, 2005
  5. Replies:
    1
    Views:
    418
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page