Constant nothingness

This is a discussion on Constant nothingness within the C Programming forums, part of the General Programming Boards category; Qualifying void as const doesn't seem to be valid at all, however GCC will accept "const void" as a legal ...

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    Constant nothingness

    Qualifying void as const doesn't seem to be valid at all, however GCC will accept "const void" as a legal type (though some compilers consider it an error). Has anyone ever encountered this in code, or can think of an interesting use for it?
    Personally, I can't see any beyond maybe writing amusing/obsfucated code...

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    I dont really see any use of it. But what I'm suprised is that there are few compiler which does happly accept that. Which is kind a wired.

    ~ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    memcmp is prototyped this way.

    Code:
     
      int memcmp(const void *s1, const void *s2, size_t n);
    Void | C FAQs
    Last edited by slingerland3g; 07-21-2009 at 04:53 PM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    There are 108 references to "const void" in the GNU C Manual, and most of them are in prototypes, eg. alphasort, fwrite, scandir, etc.

    It's not pointless at all; many commands require a const, but not all of them impose a type; since void can be anything (as opposed to "nothing"), the parameter would be "const void".
    Last edited by MK27; 07-21-2009 at 05:07 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Hmm this is interesting. Here is something which i came up with

    Code:
    #include <stdio.h>
    
    const void * foo( void );
    
    int main()
    {
        printf("%s", (char *) foo() );
        
        getchar();
        return 0;
    }
    
    
    
    const void * foo( void )
    {
          char *p = NULL;
          
          if( ( p = malloc( 10 ) ) != NULL )
          {
              strcpy(p, "hello");
              return p;
          }
    }
    ~ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    @slinger, mk, ssharish:
    To clarify, I'm speaking of "const void", not "const void *". The latter is extremely useful, pointing to a generic constant type, however the former doesn't make much sense since it is specifying a return type of nothingness which doesn't make sense to be considered "constant".

    Consider the following two prototypes:
    Code:
    const void foo( int arg );
    void bar( int arg );
    They differ only in semantics. If a function returns "nothing", can it logically claim to return it as constant?

    Yet GCC respects it as a legitimate type, consider:
    Code:
    const void foo( int arg );
    void bar( int arg );
    ...
    void (*fred)(int) = foo;
    will generate a type error.

    This is the paradox I'm getting at.
    Last edited by @nthony; 07-21-2009 at 06:01 PM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I like my nothingnesses, to be constant - it's so confusing when they switch to become a something, and then go back to being a nothing again.


  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by @nthony View Post
    const void foo( int arg );
    A prototype with a 'return' value of "const void" (as opposed to const void*) will produce a GCC warning ("function definition has qualified void return type"). If you actually try to use a return value from a void function, const or not you get an error.

    That's not the purpose of the type. I think you are chasing a straw dog, and slowly at that It just ain't enough of anything to be a "paradox".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    I get this on 4.1.2 on Debian, but 3.3.5 on Debian and 3.4.5 and 4.4.0 on MinGW are silent.

    I'm not going after a solution or anything; just offering one of those "hmm, that's weird" moments.

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    >> I'm not going after a solution or anything; just offering one of those "hmm, that's weird" moments.

    You think that's wierd? Well in C++ you can actually return void!

    Code:
    template < typename Type >
    Type foo( void )
    {
    	return Type( );
    }
    
    int main( void )
    {	
    	foo< void >( );
    }



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  4. Try out my new game :) !
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 06-05-2003, 08:10 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21