I read up and and these are the things that confuse me.
1)It says, a void pointer can hold the address of any data type. But pointer arithmetic is not allowed on it.
2)Also you cannot directly dereference it, you have to assign it to a pointer of the appropriate type to dereference it.
In that case, whats the point ? The very need of having different types of pointers is Pointer Arithmetic. for eg,
1)int* p; p++ ; - points to the location after 2 bytes
2)char* p; p++; - points to the next byte.
If you were not bothered about pointer arithmetic or dereferencing it,then , even a pointer to a float can hold the address of an integer.
For example, i tried this code, where i stored the address of an int in a pointer to a float, then passed this address back to a pointer to an integer and printed the value here. It works . So whats the point of having a void pointer ?
Code:#include<stdio.h> #include<conio.h> int main(void) { int* pi; float* pf; int* pii; int a=5,sum=0; clrscr(); pi=&a; pf=&a; pii=pf; sum=*pi; //sum=*pf; //doesnt work, shouldnt work, sum=*pii; getch(); return 0; }



8Likes
LinkBack URL
About LinkBacks



