int* pointerToInt=NULL;//This DECLARES a pointer to an int and assigns the address stored in the poitner as NULL (0)
int a=40;//This is just an int with value 40, not a pointer
int b=0;//just another int with value 0, not a pointer
float myFloat=1.0f;//just a float, not a pointer
printf("We're on line#%d and a is:%d\tb is:%d \n",__LINE__, a, b);
//a=*pointerToInt; //this would dereference (take the value that pointerToInt points to)
//and assign it to a, but I've commented it out because i'd be dereferencing null, which isn't allowed (on normal platforms)
pointerToInt=&a;//This takes the ADDRESS to a, you can now say that "pointerToInt points to a" meaning that:
b=*pointerToInt;//If we dereference it we will get the value that is contained in a
printf("We're on line#%d and a is:%d\tb is:%d \n",__LINE__, a, b);
void* pointerToVoid=NULL;//This is a pointer to void type. A void type doesn't have a size, so it's just an address
pointerToVoid=&a;//we can store any address in a void pointer but:
//b=*pointerToVoid; //This doesn't workbecause the pointer isn't specified to point to a complete object type (a struct, a primitive like float, int whatever)
a=5;//Just to make some change
printf("We're on line#%d and a is:%d\tb is:%d \n",__LINE__, a, b);
b=*((int*)pointerToVoid);//We can do this to make this work. We coerce/cast the void* to be an int*. It now makes sense to dereference it.
//(The outermost parenthesis here is uneccesary but I added it for some clarity)
printf("We're on line#%d and a is:%d\tb is:%d \n",__LINE__, a, b);
pointerToVoid=&myFloat;//since this is a void pointer we can take the address of a float and store it in a void pointer without the compiler complaining
b=*((int*)pointerToVoid);//we can then dereference it as an int, which doesn't make much sense at all
printf("We're on line#%d and a is:%d\tb is:%d \n",__LINE__, a, b);//This happens. Be careful using void pointers.
We're on line#7 and a is:40 b is:0
We're on line#12 and a is:40 b is:40
We're on line#17 and a is:5 b is:40
We're on line#20 and a is:5 b is:5
We're on line#23 and a is:5 b is:1065353216