I was just trying to use a void pointer to an integer array ,I tried to see if i can print the array back by casting it back into int. But it is giving me some random value.
The C standard does not define behaviour for arithmetic of void *
, so you need to cast your void *
to another pointer type first before doing arithmetic with it.
Some compilers [as an extension] treat pointer arithmetic of void *
the same as char *
, so each ‘+1’ will only increase the address by 1, rather than by the size of the pointed-to object. This is not standardised though so you can't rely on this behaviour.