void Variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrPickle
    New Member
    • Jul 2008
    • 100

    void Variables

    What data do void variables hold? Doesn't void mean empty, so the variable would hold nothing?

    Why would you want this unless you're using it as a function pointer?
  • curiously enough
    New Member
    • Aug 2008
    • 79

    #2
    There's no such thing as void variables.

    Comment

    • MrPickle
      New Member
      • Jul 2008
      • 100

      #3
      What's this then:
      void* Foo

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Its a pointer to an undefined type. You can not dereference a void pointer, that is following on from you example

        *Foo

        Would cause a compiler error. Void pointers are useful when the type of a pointer can not be know or is not useful to know. For instance in the return from malloc or the pointer passed to many thread creation functions as the parameter to pass to the new thread.

        You can not declare

        void Foo2;

        So there is no such thing as a void variable.

        Even a function pointer, for example

        int (*pFn)(int);

        is not a void pointer.

        Comment

        • MrPickle
          New Member
          • Jul 2008
          • 100

          #5
          I get it now, Thank you

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Now that you get it, let me confuse you! Consider the following code snippet:
            Code:
            int func(void);
            ...
            (void) func();
            ...
            Here I'm casting the return value of the function to void. That certainly makes it look like there's a data type called void. In reality, this sort of cast is a special case; it tells the compiler that you want to throw away the return value of the function. The compiler would do that anyway if you hadn't included the cast to void, so cast to void is unnecessary. Some people use it as idiom to tell lint, the compiler, and [most importantly] the maintenance programmers that they knew the function returns a value but deliberately decided to ignore it.

            It isn't my intention to advocate this style, just to alert you to it so you aren't amazed if you run into one of these casts to void.

            If you're interested in this topic you might research "void statement" in C. That will lead you to things like a bare semi-colon or an integer cast to void. These are statements that don't do anything.
            Code:
            ;
            (void) 0;

            Comment

            Working...