Pages: 1 [2]   Go Down
Author Topic: void pointers * something  (Read 1552 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, look at it like this. Void pointers are like pointers to atoms. That's only useful if you know how many atoms are in something (like a cat). But a cat* pointer points at a cat. And if you add one, it points to the next cat. You don't have to care how many atoms are in the cat. Now that's useful!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25638
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've never quite been convinced by the need for a void pointer type - a char is by definition the smallest addressable element, so is the smallest thing you can have a pointer to.
I guess void just anonymises (if such a word / verb exists) it.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 64
Posts: 2100
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

void pointers aren't overly useful, they are just required. Memory allocation systems don't allocate to a type, but an address with a data range, which is a void pointer. ( explicit 'operator new' over plain old 'new' )

Also before templates were fully formed, people wrote functions like
Code:
void DoStuff( void *v_Data, int i_Size )
  {
    //Do stuff
    return;
  }

With the only benefit for using it over typed pointers was to remove casting everything to a standard type. ( Any pointer type can be implicitly cast to void* but not the other way around ).

Also there is this example.

Code:
void Foo(void (*fp)(void *), void *q) {
  fp(q);
}

This use of void*allows Foo to take a pointer to a function and handle any type of argument.
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but that's really just a fiction that is only enforced by the compiler.
And a darned good thing it (generally) is, too.
Logged

Pages: 1 [2]   Go Up
Jump to: