anish kumar said:
but ptr is a pointer to void so if i subtract -1 from it
and then cast it to void ** and do a derefrence as below
then how it is different:
*((void **)(ptr -1))
Here is another way to think of it. Suppose the type of p is
'int *' and we are doing a conversion to 'char *'. Consider the
two expressions
((char *) p) + 1 and (char *) (p+1)
The expression on the left points one byte beyond where p points,
because sizeof (char) == 1. The expression on the right points
four bytes beyond where p points (assuming sizeof (int) == 4).
These results hold because of how pointer arithmetic is defined -
adding 1 to a pointer steps over exactly the number of bytes
needed for the type that the pointer expression points to. This
type is 'char' for the left hand expression, because of the cast,
and 'int' for the right hand expression, because of what we said
about p to start with.
Now take the case when the type of p is 'void *' and we are
converting to 'void **' :
((void **) p) + 1 and (void **) (p+1)
The expression on the left points (sizeof (void *)) bytes beyond
where p points. The expression on the right points (sizeof (void))
bytes beyond where p points. If sizeof (void *) == sizeof (void)
then these results would be the same. Do you think that these two
sizes, sizeof (void *) and sizeof (void), have the same value?
What values do you think they have?