Using void pointer to an array

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

    You need to cast arr before adding j. Here is a minimal fix:

    x = *(((int *)arr)+j);
    

    but I think it's clearer to write:

    x = ((int *)arr)[j];
    

提交回复
热议问题