Consider the below 2 declarations.
* appears next to the datatype and not next to variable
char* ptr1, * ptr2, * ptr3; //all 3 are pointers
* appears next to the variable and not next to datatype
char *ptr1,*ptr2,*ptr3; //again all 3 are pointers
Is there any difference in intepretation between the 2 declarations. I know there is no difference in the variables.
What is the rationale behind introducing void pointers?
The only difference is whitespace, they end up being pointers-to-chars. I personally use
char * ptr1, using plenty of whitespace in my code.Void pointers can point to any type of object and have no size. For example, this works (but isn’t really a good idea):
(*ptr2)will now interpret the bytes of ptr1 as a float instead of an int, giving you a different result. You can change between types. There are some uses forvoid*s in pointer-to-function typedefs, as well.In addition, if you have
char * ptr1and doptr1++, you will increment the address ptr1 refers to bysizeof(char). That’s not possible with a void*, it will give you a compiler error saying it doesn’t know what size to increment by.