A void pointer can point to any data type but the reverse is not true—
that is
,
the following
program statement would result in an error:
ptr1 = vptr; //error
To remove this error, you would have to type cast it explicitly as follows:
ptr = (int *) vptr;
Also remember that if you have to perform arithmetic on a void pointer, it is not possible
without a type cast.
Arithmetic View of Pointers
A pointer is an unsigned integer variable.
Thus, a pointer contains a numeric unsigned value, using
which it is possible to add integer values and even subtract them from a pointer.
Please note that
you cannot multiply or divide two pointers.
The main difference between a normal integer and an
integer pointer is that pointer arithmetic adds and subtracts the size of the data type to which the
pointer points.
For example, when we say add 1 to any integer pointer, it is incremented by the size of an integer
variable.
Also note that each integer datum occupies 2 bytes in memory, so if we add 1 to an
Do'stlaringiz bilan baham: |