Using void pointer to an array

后端 未结 4 1805
逝去的感伤
逝去的感伤 2021-01-01 02:20

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.

4条回答
  •  长发绾君心
    2021-01-01 02:58

    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.

提交回复
热议问题