Go Down

Topic: void pointers * something (Read 4617 times) previous topic - next topic


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!
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics


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.
I speak for myself, not Arduino.


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

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) {

This use of void*allows Foo to take a pointer to a function and handle any type of argument.
Forum Mod anyone?


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

And a darned good thing it (generally) is, too.
The art of getting good answers lies in asking good questions.

Go Up