Thread: Void Pointer and Pointer to Void

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Void Pointer and Pointer to Void

    Const Pointers and Pointer to Const are 2 different Beasts!

    does the same hold for

    Void Pointers and Pointers to Void ?

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    "void" is not a type qualifier, so "void pointer" makes no sense.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    Quote Originally Posted by OnionKnight View Post
    "void" is not a type qualifier, so "void pointer" makes no sense.
    thanx

  4. #4
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by OnionKnight View Post
    "void" is not a type qualifier, so "void pointer" makes no sense.
    Not quite.

    void pointers are generic pointers. They can't be used without being cast to something else first.

    Code:
    void *MemLocation; //LEGAL
    Yes, a void pointer and a pointer to void are the same thing.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    Quote Originally Posted by vb.bajpai View Post
    thanx
    However, wikipedia refers to both of them, so i suppose they can be used inter-changeably

    "The void pointer, or void*, is supported in ANSI C and C++ as a generic pointer type . A pointer to void can store an address to any data type, and, in C, is automatically casted to any other pointer type on assignment"


    taken from wiki

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by OnionKnight View Post
    "void" is not a type qualifier, so "void pointer" makes no sense.
    "void" is not a type, so "pointer to void" also makes no sense, as it implies you are pointing AT a void, and there's no such thing.

    This is why I prefer "void star" over either of those.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Const pointers (const type *) and pointers to const (type *const) are different because const is a type qualifier. const can go before or after the asterisks, where it means different things, as you have discovered. void is an ordinary variable type, just like int and char and all the rest. (Well, not quite, because you can declare an int and you can't declare a void.) Anyway, type *int or type *void is invalid. You can only put the pointer type before the asterisks, not afterwards, where you can put const.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    void is an ordinary variable type, just like int and char and all the rest. (Well, not quite, because you can declare an int and you can't declare a void.)
    I strongly disagree with "not quite." It's a HUGE difference. "void" is not a type. It sometimes seems to act like a type but the very fact that you cannot declare a variable of type "void" proves that it is not a type.

    Void serves two completely different purposes in the language. One, you use it to declare that a function takes no arguments or has no return value. Two (completely unrelated), you use it to specify a pointer to a generic type. In one case, it indicates "nothingness." In the other case it indicates genericness.

    It does NOT mean that this generic type is "void." There is absolutely NO RELATIONSHIP between "void" and "void *."

    In spoken conversation I call it a "void star."
    Last edited by brewbuck; 06-14-2007 at 02:13 PM.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    But void has a lot more in common with int than it does with if or while. That's all I was trying to get across.

    In spoken conversation I call it a "void star."
    I say that if I'm pronouncing a variable name, like "void star p". But I refer to it as a "void pointer" otherwise. I'm sure other people refer to it differently.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    But void has a lot more in common with int than it does with if or while. That's all I was trying to get across.
    Yes, it does resemble a typename syntactically. In fact it probably IS a typename in the formal syntax. My point, though, is that despite its SYNTACTIC similarity it doesn't serve the same purpose semantically. It's basically a new gizmo that was bolted on to the language via the type system.

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Quote Originally Posted by brewbuck View Post
    I strongly disagree with "not quite." It's a HUGE difference. "void" is not a type. It sometimes seems to act like a type but the very fact that you cannot declare a variable of type "void" proves that it is not a type.
    Being able to manipulate pointers to undeclarable types is a pretty fundamental feature of the language that extends beyond void. Except for the syntactic trick of a function which returns or accepts the void type, there's really nothing differentiating void from any other incomplete type.

    void is an incomplete type that can not be completed... and that's it. You can't really think of it as something too far outside the type system without memorizing a lot of things twice.
    Code:
    #include <stdlib.h>
    
    // Types which can not be declared are normal and necessary to the language.
    struct foo;
    
    struct foo * foomalloc (size_t size) {
       return (struct foo *) malloc(size);
    }
    
    int main (void) {
       struct foo * f = foomalloc(5);
       return 0;
    }
    And if you ask me, the fact that void is the 'nothing' when used as a parameter type makes perfect sense. The only possible type you could unambiguously use for such a notation would be a type which can not be declared, which is more or less the definition of void.
    Callou collei we'll code the way
    Of prime numbers and pings!

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can even cast something to void, like lint likes:
    Code:
    (void)printf("Hello, World!\n");
    You just can't declare a variable of type void, because what would it store?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by QuestionC View Post
    void is an incomplete type that can not be completed... and that's it. You can't really think of it as something too far outside the type system without memorizing a lot of things twice.
    [code]#include <stdlib.h>
    I hadn't thought of it that way before. Thanks. Good point. I still think I'll say "void star," though, although your argument makes "pointer to void" seem a little more valid.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I personally read "void pointer" and "pointer to void" as synonymous. Much like "int pointer" and "pointer to int".
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Void pointer use in a function
    By samadhi in forum C Programming
    Replies: 2
    Last Post: 04-08-2005, 07:27 PM
  3. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. void pointer
    By frenchfry164 in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2003, 02:31 PM