Hi,
Why declare a pointer as void if it cannot be deferenced ?
Hi,
Why declare a pointer as void if it cannot be deferenced ?
Its useful when you don't know for sure what type of data that pointer will be pointing to. For example, the fwrite function takes a void* argument so you will be able to write anything to the file:
char buffer[MAX_BYTES];
SOME_STRUCT str; // [edit: cant have a var name 'struct']
fwrite(&buffer, sizeof(buffer), 1, fp);
fwrite(&str, sizeof(str), 1, fp);
also, you can dereference a void pointer if you want, just cast it into the desired type:
void *p = ...;
*(SOME_TYPE *)p; // cast p to a pointer to some type, then dereference
Last edited by Duck-billed Pla; 01-15-2002 at 05:03 AM.
Nutshell, I think you are confusing it with setting a pointer to NULL.
As Duck-billed Pla has shown, void only means undefined.....not unaccessable.....