Go Down

Topic: void pointers * something (Read 1 time) previous topic - next topic

Nick Gammon

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!

AWOL

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.
"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.

pYro_65

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: [Select]

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: [Select]
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.

PaulS

Quote
but that's really just a fiction that is only enforced by the compiler.

And a darned good thing it (generally) is, too.

Go Up