Advertisement

Pointer to void?

Started by July 31, 2002 05:58 PM
12 comments, last by Geir 22 years, 3 months ago
As I was reading Tricks of the Windows Game Programming Gurus, I came upon a type of parameter I've never seen before. int SomeFunction(void *params = NULL); It's the 'void' part I don't understand. I know how to use void for function return types as in... void SomeFunc(int x); ...or when a function doesn't take any parameters at all: int SomeFunc(void); Anyone? Geir [edited by - Geir on July 31, 2002 7:01:11 PM]
a void pointer is a pointer to an arbitrary location in memory. If you want to know more about void pointers, do a search.


Don''t listen to me. I''ve had too much coffee.
Advertisement
Isn''t a void pointer a pointer of any type, as in it can point to int, char etc data
quote: Original post by Anonymous Poster
Isn''t a void pointer a pointer of any type, as in it can point to int, char etc data


This is true only when you cast the pointer to that type.
<a href="http://www.purplenose.com>purplenose.com
quote: Original post by Geir
It''s the ''void'' part I don''t understand. I know how to use void for function return types as in...

void is used to indicate lack of type information (such as whether a variable is of type int, float, etc). So, SomeFunction(void*) takes a pointer to a variable of unspecified type. In C, any pointer type can be implicitly cast to void*, in C++ you must code a cast to make it explicit. In other words, you can call SomeFunction(void*) with a pointer to any type. The trouble with that is you don''t know what operations you can legally perform on the variable, because you don''t know what type the variable is. Therefore, you should also have some mechanism for indicating what the real type pointed to is.

Every time you cast to void*, you are telling the compiler "I know more about the type system than you do, so I''m going to disable your ability to tell me if I do something weird with the type system." Since you almost never know more than the compiler about the type system, you should almost never use void*.
Aha! Thanks for the answers.


Geir
Advertisement
quote: Every time you cast to void*, you are telling the compiler "I know more about the type system than you do, so I'm going to disable your ability to tell me if I do something weird with the type system." Since you almost never know more than the compiler about the type system, you should almost never use void*.


Ahh Sabreman, do you need your hand held for everything you do? Beginners, Sabreman is wise in the ways of computers, especially C++, but C scares him, so don't ask him any C questions.

[edited by - BeerNutts on August 2, 2002 5:32:23 PM]

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

void * is like a pointer to an unknown bit of data. In C++ you can avoid this nastiness using inheritance and polymorphism.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
quote: Original post by BeerNutts
but C scares him, so don''t ask him any C questions.

I''ve been using C for over 10 years and don''t remember ever feeling scared of it. I''m under the impression that TOTWGPG uses C++ for its examples. All the same, thanks for your sarcasm.
quote: Original post by BeerNutts
Ahh Sabreman, do you need your hand held for everything you do?


That''s right. If you use all the features of a given language to it''s fullest, you''re not a real man. Never mind that those features can save you hours, even days of debugging time 3 years down the track when you''ve got to fix a bug in your (or worse - someone else''s) code.

If I had my way, I''d have all of you shot! codeka.com - Just click it.

This topic is closed to new replies.

Advertisement