Using void pointer to an array

后端 未结 4 1807
逝去的感伤
逝去的感伤 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:54

    You are doing pointer arithmetic on void * which is not valid in C.

    In GNU C (C with gcc extensions), it is actually permitted and the sizeof (void) is considered to be 1.

    http://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html

    "addition and subtraction operations are supported on pointers to void and on pointers to functions. This is done by treating the size of a void or of a function as 1."

提交回复
热议问题