my book said a pointer like that could convert into some other data type. Thus it should be popular but Bruce Eckel said later that we'd better avoid not using it, why ???
my book said a pointer like that could convert into some other data type. Thus it should be popular but Bruce Eckel said later that we'd better avoid not using it, why ???
Never end on learning~
One time I was walking down the street when all of the sudden a void pointer burst out of a trash can and attacked me...
They are not dangerous. But you need to be careful because casting pointers can get you into trouble if you are a newbie and don't know what it is that you are doing exactly.
Remember that different data types have different sizes. So make sure you are familiar with pointers before playing around with void *'s.Code:void example1(void *arg) { char *str = (char *)arg; *str = 'a'; } void example2(void *arg) { long *array = (long *)arg; *array = 'a'; } int main() { char str[256]; memset(str, 0, 256); //zero out the array example1(str); cout << "string value: " << str << "\n"; example2(str); cout << "long value: " << str << " (different much?)"; }
thanx. I'd better practice something about pointer first.
Never end on learning~
it's dangerous to miscast pointers, yes.
that is true of all pointers who have been cast in strange ways. void * is no different but that doesn't make the use of void * wrong. You just have to learn what makes miscasts dangerous by trial and error mostly. Have fun, "you've taken your first step to a larger world"
always looking, make an offer. get me out of this place.